文件上传
2021-05-03 08:29
标签:content als att attribute ges param version tst copy 文件上传 标签:content als att attribute ges param version tst copy 原文地址:https://www.cnblogs.com/yanghaoyu0624/p/12122110.html 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