首先实现前台界面,上传图片并保存到指定路径,而后java制做dll,利用jni调用C++代码,将保存的图片路径传给dll处理,并返回一个处理结果。css
整个环境以下:
window10 x64
jdk1.7 x86,tomcat7.0 x86
myeclipse 2014 32位
struts-2.3.16.3html
第一步:利用struts2实现多图片上传,界面以下:java
项目的目录结构以下:web
首先下载struts2,并在struts-2.3.16.3apps找到struts2-blank.war,将其解压,而后把WEB-INF目录下的全部jar包复制到这,以下图:spring
在src文件夹右键,新创建一个struts.xml(这里用直接复制struts目录下的struts.xml到项目下,编译器会报错,具体缘由我也没弄清楚),具体步骤如图:apache
struts.xml内容:segmentfault
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 该属性指定须要Struts2处理的请求后缀,该属性的默认值是action,即全部匹配*.action的请求都由Struts2处理。 若是用户须要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 --> <constant name="struts.action.extension" value="do" /> <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 --> <constant name="struts.serve.static.browserCache" value="false" /> <!-- 当struts的配置文件修改后,系统是否自动从新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 --> <constant name="struts.configuration.xml.reload" value="true" /> <!-- 开发模式下使用,这样能够打印出更详细的错误信息 --> <constant name="struts.devMode" value="true" /> <!-- 默认的视图主题 --> <constant name="struts.ui.theme" value="simple" /> <!--<constant name="struts.objectFactory" value="spring" />--> <!--解决乱码 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <constant name="struts.multipart.maxSize" value="10701096"/> <package name="struts2" extends="struts-default"> <action name="upLoad" class="com.action.upLoadAction" method="execute"> <!-- 要建立/image文件夹,不然会报找不到文件 --> <param name="savePath">/image</param> <result name="success">index.jsp</result> </action> </package> </struts>
在WEB-INF目录下新建web.xml以下图:数组
web.xml内容:浏览器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Test</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
而后在WebRoot目录下新建一个image(文件夹的名称不一样的话,须要在struts.xml进行同步修改)文件夹备用:缓存
接下来新建com.action包,在此包下新建upLoadAction.java,(如何要修更名称的话,也须要在struts.xml中修改一下配置信息),代码:
package com.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; /** * 使用数组上传多个文件 * * @author ljq * */ @SuppressWarnings("serial") public class upLoadAction extends ActionSupport{ public native int Contrast(String path1,String path2); private File[] image; //上传的文件 private String[] imageFileName; //文件名称 private String[] imageContentType; //文件类型 private String savePath; @Override public String execute() throws Exception { ServletActionContext.getRequest().setCharacterEncoding("UTF-8"); //取得须要上传的文件数组 File[] files = getImage(); String []str=new String[2]; if (files !=null && files.length > 0) { for (int i = 0; i < files.length; i++) { //创建上传文件的输出流, getImageFileName()[i] str[i]=getSavePath() + "\\" + getImageFileName()[i]; System.out.println(getSavePath() + "\\" + getImageFileName()[i]); FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getImageFileName()[i]); //创建上传文件的输入流 FileInputStream fis = new FileInputStream(files[i]); byte[] buffer = new byte[1024]; int len = 0; while ((len=fis.read(buffer))>0) { fos.write(buffer, 0, len); } fos.close(); fis.close(); } } //这一段代码是用来调用dll,进行图片对比的 try{ upLoadAction t=new upLoadAction(); System.loadLibrary("Test4"); int a=3; a=t.Contrast(str[0],str[1]); System.out.println("**************"+a); }catch(Exception exp){ System.out.println("处理图片出错!"); } return SUCCESS; } public File[] getImage() { return image; } public void setImage(File[] image) { this.image = image; } public String[] getImageFileName() { return imageFileName; } public void setImageFileName(String[] imageFileName) { this.imageFileName = imageFileName; } public String[] getImageContentType() { return imageContentType; } public void setImageContentType(String[] imageContentType) { this.imageContentType = imageContentType; } /** * 返回上传文件保存的位置 * * @return * @throws Exception */ public String getSavePath() throws Exception { return ServletActionContext.getServletContext().getRealPath(savePath); } public void setSavePath(String savePath) { this.savePath = savePath; } }
在WebRoot下新建upload.jsp,代码以下:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'upLoad.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <!-- ${pageContext.request.contextPath}/upload/execute_upload.do --> <!-- ${pageContext.request.contextPath}/upload1/upload1.do --> <!-- ${pageContext.request.contextPath}/upload2/upload2.do --> <!-- --> <form action="${pageContext.request.contextPath}/upLoad.do" enctype="multipart/form-data" method="post"> 文件1:<input type="file" name="image"><br/> 文件2:<input type="file" name="image"><br/> <input type="submit" value="上传" /> </form> </body> </html>
index.jsp代码以下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'message.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> 上传成功 <br/> <s:debug></s:debug> </body> </html>
到此为止,图片就可上传成功了
其中还有就是要注意tomcat,jdk版本要一致,我都是用的1.7对应7.0,而后myeclipse2014 32位,并且要在myeclipse中将本身的jdk1.7和tomcat配置进去,而不能用myeclipse自带的
具体配置能够参考个人另外一篇文章:
https://segmentfault.com/a/11...
第二步,制做dll,并调用,制做dll的过程在个人另外一篇文章里面有:
https://segmentfault.com/a/11...
我这里只贴出程序入口处的代码:
#include "Test.h" #include "main.h" JNIEXPORT jint JNICALL Java_com_action_upLoadAction_Contrast (JNIEnv *env, jobject obj, jstring path1, jstring path2){ //将java字符串类型转化为char* 型 const char* str1 = env->GetStringUTFChars(path1, 0); const char* str2 = env->GetStringUTFChars(path2, 0); //不能直接把const char*型做为imread()的实参,所以将char *str1复制到cap1中 char cap1[128]; char cap2[128]; strcpy(cap1, str1); strcpy(cap2, str2); //printf(cap1); //printf(cap2); cv::Mat im1 = cv::imread(cap1); cv::Mat im2 = cv::imread(cap2); int predictout = image(im1, im2); return predictout; }
头文件的代码:
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_action_upLoadAction */ #ifndef _Included_com_action_upLoadAction #define _Included_com_action_upLoadAction #ifdef __cplusplus extern "C" { #endif /* * Class: com_action_upLoadAction * Method: Contrast * Signature: (Ljava/lang/String;Ljava/lang/String;)I */ JNIEXPORT jint JNICALL Java_com_action_upLoadAction_Contrast (JNIEnv *env, jobject obj, jstring path1, jstring path2); #ifdef __cplusplus } #endif #endif
制做好的dll要放在tomcat的E:\tom1\apache-tomcat-7.0.78_x86\bin
目录下还有这几个数据文本也是