1 package com.oracle.upload;
2
3 import com.google.gson.Gson;
4 import com.google.gson.JsonObject;
5 import com.oracle.domain.ResponseUpload;
6 import com.oracle.domain.UploadBean;
7 import com.oracle.domain.UserInfo;
8 import com.oracle.service.UploadService;
9 import org.apache.commons.fileupload.FileItem;
10 import org.apache.commons.fileupload.FileUploadException;
11 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
12 import org.apache.commons.fileupload.servlet.ServletFileUpload;
13 import org.apache.commons.io.FileUtils;
14
15 import javax.servlet.ServletException;
16 import javax.servlet.annotation.WebServlet;
17 import javax.servlet.http.HttpServlet;
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.http.HttpServletResponse;
20 import java.io.*;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.UUID;
25
26 /**
27 * @Description TODO
28 * @ClassName UploadServlet
29 * @Author YHY
30 * @Date 2019/11/11 11:31
31 * @Version V1.0
32 */
33 @WebServlet(value = "/UploadServlet")
34 public class UploadServlet extends HttpServlet {
35 @Override
36 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
37 /* 处理响应乱码问题:字节流需getBytes("UTF-8") */
38 /*response.setContentType("text/html;charset=UTF-8");*/
39 /* 处理post请求乱码问题 */
40 request.setCharacterEncoding("UTF-8");
41
42 String fileName = ""; //文件名*/
43 String href = "";/*服务器存储路径*/
44 String loaclhost_file_name = "";
45 UserInfo userInfo = (UserInfo) request.getSession().getAttribute("userinfo");
46 String upload_those = userInfo.getUid();
47 String access_url = "";/*用户访问的相对路径*/
48
49 /*用于返回数据*/
50 Map map = new HashMap();
51
52 boolean flag = ServletFileUpload.isMultipartContent(request);
53 if (!flag) {
54 request.setAttribute("msg", "表单的enctype属性不正确");
55 request.getRequestDispatcher("jsp/upload.jsp").forward(request, response);
56 return;
57 }
58
59 /*缓冲字节输出流*/
60 BufferedOutputStream bos = null;
61 /*缓冲字节输入流*/
62 BufferedInputStream bis = null;
63
64 /*1、创建磁盘文件工程*/
65 DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
66 /*设置文件上传过程中缓冲区的大小*/
67 diskFileItemFactory.setSizeThreshold(3);
68 /*设置文件上传过程中超出缓冲区所产生的临时文件路径*/
69 diskFileItemFactory.setRepository(new File("D://temporary"));
70 /*2、创建一个核心解析类*/
71 ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
72 /*解决中文乱码问题*/
73 servletFileUpload.setHeaderEncoding("UTF-8");
74
75 /*3、利用核心类解析request,解析后得到多个部分。返回list集合。list集合中装的值每个部分内容(FileItem文件项)。*/
76 try {
77 List<FileItem> fileItems = servletFileUpload.parseRequest(request);
78
79 /*4、遍历list集合,会得到代表每个部分的文件上传项的对象。根据文件项判断是否是文件上传项。*/
80 for (FileItem f : fileItems) {
81 /*判断这个文件项是普通项还是文件上传项*/
82 if (f.isFormField()) {
83 /*普通项:*/
84 /*接收普通项的值,不能用request.getParameter()*/
85 /*获取普通项的key*/
86 String name = f.getFieldName();
87 /*接收普通项的值*/
88 String string = f.getString();
89 System.out.println(name + ":" + string);
90 if (name.equals("href")) {
91 href = string;
92 }
93 if (name.equals("access_url")) {
94 access_url = string;
95 }
96 }
97 }
98 /*文件上传项*/
99 for (FileItem file : fileItems) {
100 if (!file.isFormField()) {
101 /*文件上传项*/
102 /*获取文件名*/
103 loaclhost_file_name = file.getName();
104 fileName = loaclhost_file_name;
105 int index = 0;
106 if ((index = fileName.lastIndexOf("\\")) != -1) {
107 loaclhost_file_name = loaclhost_file_name.substring(index + 1);
108 }
109 loaclhost_file_name = UploadServlet.randomFileNanm(loaclhost_file_name);
110 /*往request域中存储文件名*/
111 request.setAttribute("randomFileName", loaclhost_file_name.getBytes());
112 /*获取数据源*/
113 bis = new BufferedInputStream(file.getInputStream());
114
115 /*服务器存储路径*/
116 href = href + loaclhost_file_name;
117 /*用户访问的相对路径*/
118 access_url = access_url + loaclhost_file_name;
119
120 /*明确数据源*/
121 bos = new BufferedOutputStream(new FileOutputStream(href));
122 byte[] bytes = new byte[1024];
123 int line = 0;
124 while ((line = bis.read(bytes)) != -1) {
125 bos.write(bytes, 0, line);
126 }
127 if (bos != null && bis != null) {
128 /*释放资源*/
129 bos.close();
130 bis.close();
131 }
132 /*5、删除临时文件*/
133 file.delete();
134
135 FileUtils.copyFile(new File("D:\\Java\\apache-tomcat-9.0.27\\webapps\\YHY\\images\\xishi\\" + loaclhost_file_name),
136 new File("D:\\YHY\\web\\images\\xishi\\" + loaclhost_file_name));
137
138
139 /*调用sevice方法*/
140 /*封装参数*/
141 UploadBean uploadBean = new UploadBean();
142 uploadBean.setName(fileName);
143 uploadBean.setLoaclhost_file_name(loaclhost_file_name);
144 uploadBean.setUpload_those(upload_those);
145 uploadBean.setAccess_url(access_url);
146 uploadBean.setHref(href);
147 new UploadService().insertUpload(uploadBean);
148
149 map.put("code", "0");
150 map.put("msg", "成功");
151 map.put("url", "url");
152 }
153 }
154 } catch (FileUploadException e) {
155 map.put("code", "500");
156 map.put("msg", "失败");
157 map.put("url", "url");
158 e.printStackTrace();
159 } finally {
160 if (bos != null && bis != null) {
161 /*释放资源*/
162 bos.close();
163 bis.close();
164 }
165 }
166 response.getWriter().write(new Gson().toJson(map));
167 }
168
169 @Override
170 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
171 doGet(request, response);
172 }
173
174 private static String randomFileNanm(String name) {
175 return UUID.randomUUID().toString().replace("-", "") + name;
176 }
177
178 }