spring上传文件
通过spring上传文件示例:
package edu.qlnu.mxj.action;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts2.ServletActionContext;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import com.opensymphony.xwork2.ActionContext;
import edu.qlnu.mxj.service.HomeworkService;
import edu.qlnu.mxj.service.StaffService;
@SuppressWarnings("serial")
public class UploadAction extends BaseAction {
private String hwNo;
private String hwName;
private String mes;
private String author;
private HomeworkService homeworkService;
private StaffService staffService;
public StaffService getStaffService() {
return staffService;
}
public void setStaffService(StaffService staffService) {
this.staffService = staffService;
}
public String getHwNo() {
return hwNo;
}
public void setHwNo(String hwNo) {
this.hwNo = hwNo;
}
public String getHwName() {
return hwName;
}
public void setHwName(String hwName) {
this.hwName = hwName;
}
public String getMes() {
return mes;
}
public void setMes(String mes) {
this.mes = mes;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public HomeworkService getHomeworkService() {
return homeworkService;
}
public void setHomeworkService(HomeworkService homeworkService) {
this.homeworkService = homeworkService;
}
public String uploadFile() { System.out.println("1");
HttpServletRequest request = ServletActionContext.getRequest();
// HttpServletResponse response = ServletActionContext.getResponse();
DiskFileItemFactory factory = new DiskFileItemFactory(); System.out.println("1");
@SuppressWarnings("deprecation")
String path = request.getRealPath("/upload");// 设置磁盘缓冲路径
factory.setRepository(new File(path)); System.out.println("1");
factory.setSizeThreshold(1024 * 1024);// 设置创建缓冲大小
ServletFileUpload upload = new ServletFileUpload(factory);System.out.println("1");
upload.setSizeMax(-1);// 设置上传文件限制大小,-1无上限
try { System.out.println("1");
List<FileItem> list = upload.parseRequest(request);System.out.println("1");
for (FileItem item : list) {
// String name = item.getFieldName();
if (item.isFormField()) {// 判断是否是文件流
item.getString("UTF-8"); System.out.println("1");
} else {
String value = item.getName();// 会将完整路径名传过来
int start = value.lastIndexOf("\\"); System.out.println("1");
value.substring(start + 1);
InputStream in = item.getInputStream(); System.out.println("1");
FileOutputStream fos = new FileOutputStream(displayTime() + ".zip");System.out.println("1");
byte[] b = new byte[1024];
while ((in.read(b)) != -1) {
fos.write(b);
}
in.close();
fos.close();
System.out.println("upload success!");
}
}
} catch (Exception e) {
e.printStackTrace();
}
return "upload";
}
@SuppressWarnings("unchecked")
public String upload2() throws IllegalStateException, IOException{
HttpServletRequest request = ServletActionContext.getRequest();
CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
// MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
MultipartHttpServletRequest multiRequest = resolver.resolveMultipart(request);
String hwName = multiRequest.getParameter("hwName");
String hwNo = multiRequest.getParameter("hwNo");
String mes = multiRequest.getParameter("mes");
String author = multiRequest.getParameter("author");
System.out.println(hwName+hwNo+mes+author);
//判断是否是文件
if(resolver.isMultipart(request)){
//进行转换
// MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)(request);
//获取所有文件名称
Iterator<String> it = multiRequest.getFileNames(); System.out.println("222"+multiRequest.getFileNames().hasNext());
while(it.hasNext()){ System.out.println("666");
//根据文件名称取文件
MultipartFile file = multiRequest.getFile(it.next());
String fileName = file.getOriginalFilename(); System.out.println(fileName);
String localPath = "/upload/" + fileName; System.out.println(localPath);
File newFile = new File(localPath);
//上传的文件写入到指定的文件中
file.transferTo(newFile);
}
ActionContext.getContext().getSession().put("mes", "上传成功!");
}else {
ActionContext.getContext().getSession().remove("mes");
}
return "upload";
}
public String displayTime() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = df.format(new Date());
return date;
}
}