zhuguifei
2026-03-10 2c1fd10c6fbabb8e9f0e9f07fe66fb36c008e883
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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();
        }
    }
 
}