• 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);
            }
        }
  • 相关阅读:
    链表中环的入口节点
    链表中倒数第k个节点
    调整数组顺序使奇数位于偶数前面
    53. Maximum Subarray
    42. Trapping Rain Water
    48. Rotate Image
    css技巧一
    html语义
    label标签表单响应
    CSS清除浮动
  • 原文地址:https://www.cnblogs.com/hhls/p/5603052.html
Copyright © 2020-2023  润新知