package com.idt.ios.platform.app.mvc;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.idt.ios.platform.app.dao.ProjectFileUploadDao;
@Controller
public class ProjectFileUploadController {
private static final Logger log = LoggerFactory.getLogger(ProjectFileUploadController.class);
private ProjectFileUploadDao pfuDao;
@Resource(name="projectFileUploadDao")
public void setPfuDao(ProjectFileUploadDao pfuDao) {
this.pfuDao = pfuDao;
}
@SuppressWarnings("unchecked")
@RequestMapping(value = { "/uploader.do" }, method = { RequestMethod.GET, RequestMethod.POST })
@ResponseBody
public void getZpInfo(HttpServletRequest request,HttpServletResponse response) throws Exception{
response.setCharacterEncoding("UTF-8");
Integer schunk = null;//分割块数
Integer schunks = null;//总分割数
String name = null;//文件名
String uploadPath = "F:/document";
String fileOrig = request.getParameter("fileOrig");
String projectId = request.getParameter("projectId");
if ("p".equals(fileOrig)) {
uploadPath += "/project";
} else {
uploadPath += "/expert_advice/";
}
File up = new File(uploadPath);
if(!up.exists()){
up.mkdir();
}
BufferedOutputStream outputStream=null;
if (ServletFileUpload.isMultipartContent(request)) {
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1024);
//actory.setRepository(new File(repositoryPath));//设置临时目录
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
upload.setSizeMax(5 * 1024 * 1024);//设置附近大小
List<FileItem> items = upload.parseRequest(request);
//生成新文件名
String newFileName = null;
for (FileItem item : items) {
if (!item.isFormField()) {// 如果是文件类型
name = item.getName();// 获得文件名
newFileName = UUID.randomUUID().toString().replace("-","").concat(".").concat(FilenameUtils.getExtension(name));
if (name != null) {
//String nFname = newFileName;
if (schunk != null) {
name = schunk + "_" + name;
}
File savedFile = new File(uploadPath, name);
item.write(savedFile);
}
pfuDao.insertFile(projectId, uploadPath, name, fileOrig);
} else {
//判断是否带分割信息
if (item.getFieldName().equals("chunk")) {
schunk = Integer.parseInt(item.getString());
}
if (item.getFieldName().equals("chunks")) {
schunks = Integer.parseInt(item.getString());
}
}
}
if (schunk != null && schunk + 1 == schunks) {
outputStream = new BufferedOutputStream(new FileOutputStream(new File(uploadPath, newFileName)));
//遍历文件合并
for (int i = 0; i < schunks; i++) {
File tempFile=new File(uploadPath,i+"_"+name);
byte[] bytes=FileUtils.readFileToByteArray(tempFile);
outputStream.write(bytes);
outputStream.flush();
tempFile.delete();
}
outputStream.flush();
}
response.getWriter().write("{\"status\":true,\"newName\":\""+newFileName+"\"}");
} catch (FileUploadException e) {
e.printStackTrace();
log.error(e.getMessage());
response.getWriter().write("{\"status\":false}");
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
response.getWriter().write("{\"status\":false}");
}finally{
try {
if(outputStream!=null)
outputStream.close();
} catch (IOException e) {
log.error(e.getMessage());
e.printStackTrace();
}
}
}
//System.out.println("1111111111111111");
}
}
评论6
最新资源