package com.shlanbao.tzsc.utils.tools;
|
|
import java.io.File;
|
import java.io.IOException;
|
import java.net.URLEncoder;
|
import java.util.*;
|
|
import javax.servlet.ServletConfig;
|
import javax.servlet.ServletException;
|
import javax.servlet.http.HttpServlet;
|
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 com.alibaba.fastjson.JSON;
|
/**
|
*
|
* @author liuligong
|
* @create 2014-8-14下午07:03:44
|
* @return
|
*/
|
public class UploadServlet extends HttpServlet {
|
private static final long serialVersionUID = 1L;
|
String uploadPath;
|
//String OpenOffice_HOME=""; //openOffice软件安装目录
|
String upload_pdf="";
|
private static final ResourceBundle bundle = ResourceBundle.getBundle("config");
|
|
public UploadServlet() {
|
super();
|
}
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
doPost(request, response);
|
}
|
|
/**
|
* [功能说明]:pms文档管理-上传
|
* @author wanchanghuang
|
* @createTime 2016年6月6日16:57:51
|
* */
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
response.setCharacterEncoding("UTF-8");
|
String urlname = null;
|
FileItem item = null;
|
|
String oldfileName="";//原有文件名
|
String fileUrl = "";
|
//BufferedOutputStream outputStream = null;
|
Map<String, Object> m = new HashMap<String, Object>();
|
|
m.put("status",false);
|
if (ServletFileUpload.isMultipartContent(request)) {
|
try {
|
DiskFileItemFactory factory = new DiskFileItemFactory();
|
//factory.setSizeThreshold(1024);
|
// factory.setRepository(new File(repositoryPath));// 设置临时目录
|
ServletFileUpload upload = new ServletFileUpload(factory);
|
upload.setHeaderEncoding("UTF-8");
|
upload.setFileSizeMax(50*1024*1024);
|
upload.setSizeMax(50 * 1024 * 1024);// 设置附件最大大小,超过这个大小上传会失败
|
List<FileItem> items = upload.parseRequest(request);
|
|
|
for (FileItem fi : items) {
|
if(fi.getName()!=null){
|
item = fi;
|
}else{
|
urlname = fi.getString();
|
}
|
}
|
if (item!=null){
|
oldfileName = item.getName();
|
urlname = urlname==null?oldfileName:urlname;
|
String uuid = UUID.randomUUID().toString();
|
String suffix = oldfileName.substring(oldfileName.lastIndexOf("."));
|
File dir = new File(uploadPath);
|
if(!dir.exists() && !dir.isDirectory()){
|
dir.mkdirs();
|
}
|
|
File savedFile = new File(dir, urlname);
|
|
item.write(savedFile);
|
|
// 加入filePreview转换队列 ,后台进行转换
|
List<HTTPParam> list = new ArrayList<>();
|
fileUrl = uploadPath.substring(uploadPath.indexOf(":")+1)+"/"+ urlname;
|
HTTPParam fileDir = new HTTPParam("url", bundle.getString("service_url")+fileUrl);
|
list.add(fileDir);
|
try {
|
String rUrl = bundle.getString("file_preview_url");
|
rUrl+="/addTask";
|
URLEncoder.encode(rUrl,"utf-8");
|
HTTPSend.sendGet(rUrl, list);
|
|
} catch (IOException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
|
}
|
|
|
//将office文件转换成PDF文件
|
// Offic2PDFUtil.office2PDF(uploadPath+"/"+oldfileName, upload_pdf+"/"+newfileName,OpenOffice_HOME);
|
m.put("status", true);
|
m.put("fileUrl", fileUrl );
|
response.getWriter().write(JSON.toJSONString(m));
|
|
} catch (FileUploadException e) {
|
e.printStackTrace();
|
m.put("status", false);
|
response.getWriter().write(JSON.toJSONString(m));
|
} catch (Exception e) {
|
e.printStackTrace();
|
m.put("status", false);
|
response.getWriter().write(JSON.toJSONString(m));
|
|
}
|
}
|
}
|
|
|
/***
|
* [功能说明]:将office文件保存为-pdf文件
|
* @author wanchanghuang
|
* @createTime 2016年6月6日17:01:13
|
* */
|
|
@Override
|
public void init(ServletConfig config) throws ServletException {
|
// repositoryPath = FileUtils.getTempDirectoryPath();
|
uploadPath =bundle.getString("save_url")+ bundle.getString("upload");
|
upload_pdf=bundle.getString("save_url")+ bundle.getString("upload_pdf");
|
//OpenOffice_HOME=bundle.getString("OpenOffice_HOME");
|
File up = new File(uploadPath);
|
if (!up.exists()) {
|
up.mkdir();
|
}
|
}
|
|
}
|