web.xml
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">
01_图片异步上传
index.html
index.htm
index.jsp
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
struts.xml
/p>
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
CustomerAction.java
package com.itkawa.upload;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.itkawa.crm.utils.UploadUtils;
import com.opensymphony.xwork2.ActionSupport;
public class CustomerAction extends ActionSupport {
private static final long serialVersionUID = -3281746108664320233L;
// 图片上传
private File image_upload;// 上传后的文件
private String image_uploadContentType;// 类型
private String image_uploadFileName;// 上传的文件名称
// 读取配置中的properties
private String uploadDir ="F:/crm/images";
public File getImage_upload() {
return this.image_upload;
}
public void setImage_upload(File image_upload) {
this.image_upload = image_upload;
}
public String getImage_uploadContentType() {
return this.image_uploadContentType;
}
public void setImage_uploadContentType(String image_uploadContentType) {
this.image_uploadContentType = image_uploadContentType;
}
public String getImage_uploadFileName() {
return this.image_uploadFileName;
}
public void setImage_uploadFileName(String image_uploadFileName) {
this.image_uploadFileName = image_uploadFileName;
}
public String do_saveImage() {
System.out.println("contentType : " + image_uploadContentType);
System.out.println("fileName : " + image_uploadFileName);
try {
String stuffix = image_uploadFileName.substring(image_uploadFileName.lastIndexOf("."));
// 文件名称命名
String path = UploadUtils.getPath(image_upload).concat(stuffix);
File file = new File(uploadDir + path);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
FileUtils.copyFile(image_upload, file);
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("{'path':'"+path+"'}"); // 返回json , 图片路径信息
} catch (IOException e) {
return NONE;
}
return NONE;
}
}
index.jsp
pageEncoding="UTF-8"%>
Insert title here$(function() {
/*============== 异步上传文件 start ========================*/
$('#btn_submit').on('click', function() {
var fileName = $("#upload").val();
if (fileName == "") {
console.log("=== 没有选择文件 ====")
return;
}
//alert("fileName = "+fileName);
$.ajaxFileUpload({
url : '${pageContext.request.contextPath }/customer/saveImage',
secureuri : false,
fileElementId : 'image_upload',//file标签的id
type : 'post',
dataType : 'json', //返回值类型 一般设置为json
success : function(data, status) //服务器成功响应处理函数
{
console.log(data);
var src = "${pageContext.request.contextPath }/images"+data.path;
$("#img").attr("src",src);
},
error : function(data, status, e)//服务器响应失败处理函数
{
alert("服务器挂了....需要重启...");
}
});
});
/*============== 异步上传文件 end ========================*/
});
在tomcat配置文件里添加虚拟路径
Context docBase="F:/crm/images" path="/upload/images"/>