common upload乱码

Posted hhls

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了common upload乱码相关的知识,希望对你有一定的参考价值。

 

 

    1. request.setCheracterEncoding("utf-8");   
    2. DiskFileUpload.setHeaderEncoding("utf-8");   
    3. FileItem.getString("utf-8");  
      上面这三个地方设置好之后就应该解决了。
      package onlineTest.server;
      
      import java.io.File;
      import java.io.IOException;
      import java.util.List;
      
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      
      import onlineTest.bean.User;
      import onlineTest.dao.UserDao;
      
      import org.apache.commons.fileupload.FileItem;
      import org.apache.commons.fileupload.disk.DiskFileItemFactory;
      import org.apache.commons.fileupload.servlet.ServletFileUpload;
      
      public class ReceiveFile extends HttpServlet {
      
          private String uploadPath = "uploadpic/upload/"; // 上传文件的目录
          private String tempPath = "uploadpic/uploadtmp/"; // 临时文件目录
          /* private String serverPath = null; */
          private String[] fileType = new String[] { ".jpg", ".gif", ".bmp", ".png",".jpeg", ".ico" };
          private int sizeMax = 5;// 图片最大上限
      
          @Override
          protected void doPost(HttpServletRequest request,
                  HttpServletResponse response) throws ServletException, IOException {
              request.setCharacterEncoding("utf-8");  
              // 服务器端根目录
              String serverPath = getServletContext().getRealPath("/").replace("\\","/");
              System.out.println("地址+" + serverPath);
              // Servlet初始化时执行,如果上传文件目录不存在则自动创建
              if (!new File(serverPath + uploadPath).isDirectory()) {
                  new File(serverPath + uploadPath).mkdirs();
              }
              if (!new File(serverPath + tempPath).isDirectory()) {
                  new File(serverPath + tempPath).mkdirs();
              }
      
              DiskFileItemFactory factory = new DiskFileItemFactory();
              factory.setSizeThreshold(5 * 1024); // 最大缓存
              factory.setRepository(new File(serverPath + tempPath));// 临时文件目录
      
              ServletFileUpload upload = new ServletFileUpload(factory);
              upload.setSizeMax(sizeMax * 1024 * 1024);// 文件最大上限
              upload.setHeaderEncoding("utf-8");
              String filePath = null;
              try {// 解析request对象中的表单项
                  List<FileItem> items = upload.parseRequest(request);// 获取所有文件列表 ; list中是FileItem对象
                  // 提取文本
                  for (int i = 0; i < items.size(); i++) {
                      // 里面一个for循环,获取一行的数据
                      FileItem item = items.get(i);
                      
                      if (!item.isFormField()) {// 文件名 &nbsp; 表单文本项
                          String fileName = item.getName().toLowerCase();
                          item.getString("utf-8");
                          if (fileName.endsWith(fileType[0])
                                  || fileName.endsWith(fileType[1])
                                  || fileName.endsWith(fileType[2])
                                  || fileName.endsWith(fileType[3])
                                  || fileName.endsWith(fileType[4])
                                  || fileName.endsWith(fileType[5])) {
                              filePath = serverPath + uploadPath + fileName;
                              
                              // System.out.println(filePath);
                              File file = new File(filePath);
                              item.write(file);
                              System.out.println("fileName+" + fileName);
                              System.out.println("filePath+" + filePath);
                              System.out.println(getClass().getResource("/").getFile().toString());
                              
                              User u =new User();
                              u=(User)request.getSession().getAttribute("user");
                              u.setUrl(fileName);
                              int result = UserDao.addFavicon(u);
                              if (result==0) {
                                  request.setAttribute("errorMsg",
                                          "上传失败,请确认上传的文件存在并且类型是图片!");    
                              } else {
                                  request.setAttribute("errorMsg", "上传成功,请右键点击刷新(或者按F5)");                        
                              }
                              request.getRequestDispatcher(
                                      "web/common/show/uploaderror.jsp").forward(
                                      request, response);
                          } else {
                              request.setAttribute("errorMsg",
                                      "上传失败,请确认上传的文件存在并且类型是图片!");
                              request.getRequestDispatcher(
                                      "web/common/show/uploaderror.jsp").forward(
                                      request, response);
                          }
                      } else {
                          // 非文件流
                          String value = item.getString();
                          value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
                          // System.out.println(value);
                          System.out.println(value);
                      }
      
                  }
              } catch (Exception e) {
                  e.printStackTrace();
                  request.setAttribute("errorMsg", "上传失败,请确认上传的文件存在并且类型是图片!");
                  request.getRequestDispatcher("web/common/show/uploaderror.jsp")
                          .forward(request, response);
              }
          }
      
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                  throws ServletException, IOException {
              this.doPost(req, resp);
          }
      }

       

以上是关于common upload乱码的主要内容,如果未能解决你的问题,请参考以下文章

已解决在react+ts中 atnd 用 upload 组件报错Failed to execute ‘readAsArrayBuffer,param 1 is notof type Blob(代码片段

read ECONNRESET at TLSWrap.onStreamRead (internal/stream_base_commons.js:209:20) { errno: -4077(代码片段

org.apache.commons.net.ftp.FTPClient下载中文文件夹乱码如何处理?

用commons-email,javax.mail发送邮件,中文附件名乱码。

hi模板文件报乱码问题

java base64解密乱码问题 代码如下: