ajax上传图片和Excel
html:
<form class="adminAdd-list" id="uploadForm">
<input type='file' id='upload' name="idcard" accept='image/*' multiple='multiple' style="display: none">
</form>
js:
var formData = new FormData($( "#uploadForm" )[0]);
$.ajax({
url: url,
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (data) {
},
error: function () {
}
});
java
package com.sumavision.po.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.model.InternalWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import com.opensymphony.xwork2.ActionSupport;
import com.sumavision.po.common.ResponseInfo;
import com.sumavision.po.entity.Account;
import com.sumavision.po.entity.AccountEmployeeRef;
import com.sumavision.po.entity.Customer;
import com.sumavision.po.entity.Employee;
import com.sumavision.po.entity.EmployeeRecord;
import com.sumavision.po.service.AccountEmployeeRefService;
import com.sumavision.po.service.AccountService;
import com.sumavision.po.service.EmployeeRecordService;
import com.sumavision.po.service.EmployeeService;
import com.sumavision.po.util.CommonUtil;
import com.sumavision.po.util.ImageCompress;
@Namespace("/sd")
public class FileUploadController extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
Logger logger = Logger.getLogger(FileUploadController.class);
private File idcard; //上传的文件
private String idcardFileName; //保存原始文件名
@Action(value = "idCardUpload")
public void getAccountList() throws FileNotFoundException, IOException {
logger.info("idCardUpload()");
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
ResponseInfo responseInfo = new ResponseInfo();
Map<String, Object> data = new HashMap<String, Object>();
String result = "";
String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload/idcard");
System.out.println("path=====================================>" + path);
String filename = path+File.separator+idcardFileName;
System.out.println("filename==========================" + filename);
// 获取时间戳
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String str = sdf.format(date);
String filePath = path+ File.separator + str + idcardFileName.substring(idcardFileName.lastIndexOf("."));
String newFilePath = path + File.separator + str + idcardFileName.substring(idcardFileName.lastIndexOf("."));
File dest = new File(filePath);
System.out.println("dest=============================>" + dest.getAbsolutePath());
FileUtils.copyFile(idcard, dest);///zzzzzzzzzzz
File oldFile=new File(filePath);
File newFile=new File(newFilePath);
System.out.println("oldFile.getAbsolutePath():"+oldFile.getAbsolutePath()+" "+oldFile.getName());
System.out.println("newFile.getAbsolutePath():"+newFile.getAbsolutePath()+" "+newFile.getName());
ImageCompress.zipImageFile(oldFile, newFile);
try {
data.put("filepath", newFilePath);
responseInfo.setData(data);
responseInfo.setStatus("0");
responseInfo.setErrorMessage("test");
result = responseInfo.getResponseStr();
} catch (Exception e) {
responseInfo.setData(data);
responseInfo.setStatus("-1");
responseInfo.setErrorMessage("获取fileupload发生异常");
result = responseInfo.getResponseStr();
logger.info("获取fileupload发生异常");
logger.error(e);
}
CommonUtil.responseStr(response, result);
logger.info("getAccountList结束");
}
public File getIdcard() {
return idcard;
}
public void setIdcard(File idcard) {
this.idcard = idcard;
}
public String getIdcardFileName() {
return idcardFileName;
}
public void setIdcardFileName(String idcardFileName) {
this.idcardFileName = idcardFileName;
}
}