001 | package anubis.upload; |
002 |
003 | import java.io.BufferedOutputStream; |
004 | import java.io.File; |
005 | import java.io.FileOutputStream; |
006 | import java.io.IOException; |
007 | import java.util.List; |
008 | import java.util.UUID; |
009 |
010 | import javax.servlet.ServletConfig; |
011 | import javax.servlet.ServletException; |
012 | import javax.servlet.http.HttpServlet; |
013 | import javax.servlet.http.HttpServletRequest; |
014 | import javax.servlet.http.HttpServletResponse; |
015 |
016 | import org.apache.commons.fileupload.FileItem; |
017 | import org.apache.commons.fileupload.FileUploadException; |
018 | import org.apache.commons.fileupload.disk.DiskFileItemFactory; |
019 | import org.apache.commons.fileupload.servlet.ServletFileUpload; |
020 | import org.apache.commons.io.FileUtils; |
021 | import org.apache.commons.io.FilenameUtils; |
022 |
023 | /** |
024 | * @项目名称: plupload |
025 | * @类名称: UploaderServlet |
026 | * @类描述: 上传后台处理 |
027 | * @创建人: serical |
028 | * @创建时间: 2015-9-18 下午4:21:14 |
029 | * @修改人: serical |
030 | * @修改时间: 2015-9-18 下午4:21:14 |
031 | * @修改备注: |
032 | * @version: 1.0 |
033 | */ |
034 | public class UploaderServlet extends HttpServlet { |
035 |
036 | private static final long serialVersionUID = 1L; |
037 | String repositoryPath; |
038 | String uploadPath; |
039 |
040 | /** |
041 | * 上传处理 |
042 | * @方法名:doPost |
043 | * @参数:@param request |
044 | * @参数:@param response |
045 | * @参数:@throws ServletException |
046 | * @参数:@throws IOException |
047 | * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) |
048 | */ |
049 | @SuppressWarnings ( "unchecked" ) |
050 | public void doPost(HttpServletRequest request, HttpServletResponse response) |
051 | throws ServletException, IOException { |
052 | response.setCharacterEncoding( "UTF-8" ); |
053 | Integer schunk = null ; //分割块数 |
054 | Integer schunks = null ; //总分割数 |
055 | String name = null ; //文件名 |
056 | BufferedOutputStream outputStream= null ; |
057 | if (ServletFileUpload.isMultipartContent(request)) { |
058 | try { |
059 | DiskFileItemFactory factory = new DiskFileItemFactory(); |
060 | factory.setSizeThreshold( 1024 ); |
061 | factory.setRepository( new File(repositoryPath)); //设置临时目录 |
062 | ServletFileUpload upload = new ServletFileUpload(factory); |
063 | upload.setHeaderEncoding( "UTF-8" ); |
064 | upload.setSizeMax( 1024 * 1024 * 1024 ); //设置文件最大值 |
065 | List<FileItem> items = upload.parseRequest(request); |
066 | //生成新文件名 |
067 | String newFileName = null ; |
068 | for (FileItem item : items) { |
069 | if (!item.isFormField()) { // 如果是文件类型 |
070 | name = item.getName(); // 获得文件名 |
071 | newFileName = UUID.randomUUID().toString().replace( "-" , "" ).concat( "." ).concat(FilenameUtils.getExtension(name)); |
072 | if (name != null ) { |
073 | String nFname = newFileName; |
074 | if (schunk != null ) { |
075 | nFname = schunk + "_" + name; |
076 | } |
077 | File savedFile = new File(uploadPath, nFname); |
078 | item.write(savedFile); |
079 | } |
080 | } else { |
081 | //判断是否带分割信息 |
082 | if (item.getFieldName().equals( "chunk" )) { |
083 | schunk = Integer.parseInt(item.getString()); |
084 | } |
085 | if (item.getFieldName().equals( "chunks" )) { |
086 | schunks = Integer.parseInt(item.getString()); |
087 | } |
088 | if (item.getFieldName().equals( "fileName" )) { |
089 | System.out.println(item.getString()); |
090 | } |
091 | } |
092 | } |
093 | |
094 | if (schunk != null && schunk + 1 == schunks) { |
095 | outputStream = new BufferedOutputStream( new FileOutputStream( new File(uploadPath, newFileName))); |
096 | //遍历文件合并 |
097 | for ( int i = 0 ; i < schunks; i++) { |
098 | File tempFile= new File(uploadPath,i+ "_" +name); |
099 | byte [] bytes=FileUtils.readFileToByteArray(tempFile); |
100 | outputStream.write(bytes); |
101 | outputStream.flush(); |
102 | tempFile.delete(); |
103 | } |
104 | outputStream.flush(); |
105 | } |
106 | response.getWriter().write( "{\"status\":true,\"newName\":\"" +newFileName+ "\"}" ); |
107 | } catch (FileUploadException e) { |
108 | e.printStackTrace(); |
109 | response.getWriter().write( "{\"status\":false}" ); |
110 | } catch (Exception e) { |
111 | e.printStackTrace(); |
112 | response.getWriter().write( "{\"status\":false}" ); |
113 | } finally { |
114 | try { |
115 | if (outputStream!= null ) |
116 | outputStream.close(); |
117 | } catch (IOException e) { |
118 | e.printStackTrace(); |
119 | } |
120 | } |
121 | } |
122 | } |
123 |
124 | @Override |
125 | public void init(ServletConfig config) throws ServletException { |
126 | repositoryPath = FileUtils.getTempDirectoryPath(); |
127 | uploadPath = config.getServletContext().getRealPath(config.getInitParameter( "uploadPath" )); |
128 | File up = new File(uploadPath); |
129 | if (!up.exists()){ |
130 | up.mkdir(); |
131 | } |
132 | } |
133 | } |

oldfox LV19
2022年2月22日
kinguui LV1
2021年9月3日
xuexizhuanyong23 LV16
2021年3月2日
lyd19931203 LV21
2020年6月16日
bjgaocl LV13
2020年3月21日
skipple3 LV39
2020年3月3日
sun0224yu LV3
2020年1月6日
gggame LV1
2019年12月17日
6301339 LV2
2019年12月1日
jellyprince LV2
2019年11月17日