[Java] 图片上传 →→→→→进入此内容的聊天室

来自 , 2020-12-19, 写在 Java, 查看 141 次.
URL http://www.code666.cn/view/632cee94
  1. -----------------------------jsp页面--------------------------------------------------------------
  2. <%@ page language="java" contentType="text/html; charset=UTF-8"
  3.     pageEncoding="UTF-8"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>上传文件</title>
  9. </head>
  10. <body>
  11.         <form action="FileUpload" method="post" enctype="MULTIPART/FORM-DATA">
  12.                 参数:<input type="text" name="para1" /><br>
  13.                 上传文件:<input type="file" name="aa" /><br>
  14.                 另选文件:<input type="file" name="bb" /><br>
  15.                 再选文件:<input type="file" name="cc" /><br>
  16.                 <input type="submit" value="提交" />
  17.         </form>
  18. </body>
  19. </html>
  20. -----------------------------后台serverlet--------------------------------------------------------------
  21.  
  22. package com.cheletong.servlet;
  23.  
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.io.PrintWriter;
  27. import java.text.SimpleDateFormat;
  28. import java.util.Date;
  29. import java.util.List;
  30. import java.util.Random;
  31.  
  32. import javax.servlet.annotation.WebServlet;
  33. import javax.servlet.http.HttpServlet;
  34. import javax.servlet.http.HttpServletRequest;
  35. import javax.servlet.http.HttpServletResponse;
  36.  
  37. import net.sf.json.JSONArray;
  38. import net.sf.json.JSONObject;
  39.  
  40. import org.apache.commons.fileupload.FileItem;
  41. import org.apache.commons.fileupload.FileUploadException;
  42. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  43. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  44.  
  45. /**
  46.  * Servlet implementation class MaterialManage
  47.  */
  48.  
  49. @WebServlet("/FileUpload")
  50. public class FileUpload extends HttpServlet {
  51.  
  52.         /**
  53.          *
  54.          */
  55.         private static final long serialVersionUID = 1L;
  56.  
  57.         /**
  58.          * @throws IOException
  59.          * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
  60.          *      response)
  61.          */
  62.         protected void doPost(HttpServletRequest request,
  63.                         HttpServletResponse response) throws IOException {
  64.                 JSONObject resultJson = new JSONObject();
  65.                 JSONArray paths = new JSONArray();
  66.                 Random ran = new Random();
  67.                 int respCode = -1;
  68.                 try {
  69.                         request.setCharacterEncoding("utf-8");
  70.                         response.setContentType("text/html;charset=utf-8");
  71.  
  72.                         String uploadPath = getServletContext().getRealPath("/upload");
  73.                         System.out.println(uploadPath);
  74.                         String tempPath = uploadPath + "\\temp";
  75.                         String today = new SimpleDateFormat("yyyyMMdd").format(new Date());
  76.                         String todayPath = uploadPath + "\\" + today;
  77.  
  78.                         if (uploadPath == null) {
  79.                                 return;
  80.                         }
  81.  
  82.                         File uploadDir = new File(uploadPath);
  83.                         File tempDir = new File(tempPath);
  84.                         File todayDir = new File(todayPath);
  85.  
  86.                         if (!uploadDir.exists()) {
  87.                                 uploadDir.mkdir();
  88.                         }
  89.                         if (!tempDir.exists()) {
  90.                                 tempDir.mkdir();
  91.                         }
  92.                         if (!todayDir.exists()) {
  93.                                 todayDir.mkdir();
  94.                         }
  95.  
  96.                         if (!ServletFileUpload.isMultipartContent(request)) {
  97.                                 return;
  98.                         }
  99.  
  100.                         DiskFileItemFactory factory = new DiskFileItemFactory();
  101.                         factory.setRepository(tempDir);
  102.                         factory.setSizeThreshold(1024 * 1024 * 1);
  103.  
  104.                         ServletFileUpload sfu = new ServletFileUpload(factory);
  105.                         sfu.setHeaderEncoding("utf-8");
  106.  
  107.                         List<FileItem> fileItems = null;
  108.                         try {
  109.                                 fileItems = sfu.parseRequest(request);
  110.                         } catch (FileUploadException e) {
  111.                                 e.printStackTrace();
  112.                                 return;
  113.                         }
  114.                         int i;
  115.                         for (i = 0; i < fileItems.size(); i++) {
  116.                                 FileItem item = fileItems.get(i);
  117.                                 if (!item.isFormField()) {
  118.                                         String itemFullName = item.getName();
  119.                                         if (itemFullName.length() > 0) {
  120.                                                 String fileExtension = itemFullName
  121.                                                                 .substring(itemFullName.lastIndexOf("."));
  122.                                                 String fileNewName = new SimpleDateFormat(
  123.                                                                 "yyyyMMdd_HHmmss").format(new Date())
  124.                                                                 + "_"
  125.                                                                 + Math.abs(ran.nextInt()) + fileExtension;
  126.  
  127.                                                 File file = new File(todayPath, fileNewName);
  128.                                                 try {
  129.                                                         item.write(file);
  130.                                                         String name = "/upload/" + today + "/"
  131.                                                                         + file.getName();                                                      
  132.                                                         JSONObject path = new JSONObject();
  133.                                                         path.put("Path", name);
  134.                                                         paths.add(path);
  135.                                                 } catch (Exception e) {
  136.                                                         e.printStackTrace();
  137.                                                 }
  138.                                         }
  139.                                 }
  140.                         }
  141.                         if(i == fileItems.size()) {
  142.                                 respCode = 0;
  143.                         }
  144.                 } catch (Exception e) {
  145.                         e.printStackTrace();
  146.                 } finally {
  147.                         PrintWriter pw = response.getWriter();
  148.                         resultJson.put("response", respCode);
  149.                         resultJson.put("data", paths);
  150.                         pw.write(resultJson.toString());
  151.                         pw.flush();
  152.                         pw.close();
  153.                 }
  154.         }
  155.  
  156.         /**
  157.          * @throws IOException
  158.          * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
  159.          *      response)
  160.          */
  161.         protected void doGet(HttpServletRequest request,
  162.                         HttpServletResponse response) throws IOException {
  163.                 doPost(request, response);
  164.         }
  165.  
  166. }
  167.  

回复 "图片上传"

这儿你可以回复上面这条便签

captcha