上传下载全局异常处理和ajax专题

2019-04-15 14:51发布

3大块较重要的东西分散在各处,复制粘贴到一起


servlet上传:

前端3要素: multipart/form-data post type=file        
    
        upload
        
        
    
这个请求会先通过baseservlet处理,得到method(pic)后调用productservlet的pic方法,
但是enctype="multipart/form-data"的表单无法通过request.getParameter获取,
所以action=/d2/product然后用隐藏域提交method=pic会无法进入pic方法,
这里写成了/d2/product?method=pic。

后台代码(fc,upload,fileitem都来自commons-fileupload包):
public String pic(HttpServletRequest r, HttpServletResponse s) throws Exception {
        String u = r.getParameter("u");
        String fn = r.getParameter("fn");
            //始终打印null --null
        System.out.println(u + " --" + fn);
        DiskFileItemFactory fc = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(fc);
        List list = upload.parseRequest(r);
        Map> map = upload.parseParameterMap(r);
            //list能获取到,map是null
        System.out.println(list);
        System.out.println(map);
        for (FileItem item : list) {
                // 文件的输出是            fn:一堆乱码        01.jpg:false
                // u=1111的输出是        u:u1111         null:true
            System.out.print(item.getFieldName() + ":" + item.getString());
            System.out.println(" "+item.getName() + ":" + item.isFormField());
            if (!item.isFormField()) {
                File file = new File( "e:/" + item.getName());
                InputStream in = item.getInputStream();
                FileOutputStream out = new FileOutputStream(file);
                byte[] b = new byte[1024];
                int len = 0;
                while ((len = in.read(b)) != -1) {
                    out.write(b, 0, len);
                }
                in.close();
                out.close();
            }
        }
        return null;
    }

===================================================

serlvet下载并解决中文文件名乱码问题(包括火狐浏览器):    
重点两个:1个是结论文件名乱码的通用方法2,更重要的是mimetype设置成application/octet-stream可以通用的处理下载文件,即s.setHeader("content-type", "application/octet-stream");

    public class S2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest r, HttpServletResponse s) throws ServletException, IOException {
    
        ServletContext sc = getServletContext();
            //filename是乱码,因为浏览器的中文经过utf8编码传给服务器,
            //服务器在is08859-1编码得到filename,所以还原需要用同样的方法
        String filename = r.getParameter("filename");

            //用is08859-1解码再用utf-8编码,得到原文件名filename1。方法1:
        //byte[] bytes = filename.getBytes("iso8859-1");
        //String filename1 = new String(bytes, "utf-8");
            //方法2:
            //两种方法都能得到原文件名,似乎方法2更好,因为得到f3也是字符串
        String f3 = URLEncoder.encode(filename, "iso8859-1");
        String filename1 = URLDecoder.decode(f3, "utf-8");
                
        byte[] b = new byte[1024];
        String p = sc.getRealPath("myjpg/" + filename1);
        String mimeType = sc.getMimeType(p);
            //这里根据文件类型得到mimetype,根路径无关,jpg的mimetype是image/jpeg

        s.setHeader("content-type", mimeType);                //必须设置1
        //s.setContentType("application/octet-stream");
        //设置成application/octet-stream更好,适用于任意类型文件
            
            
        //String cd = "attachment;filename=" + filename1;        //头1
        String cd = "attachment;filename*=utf-8'zh_cn'" + f3;    //头2
            //一般浏览器用头1就可以解决中文乱码问题,但火狐不行,通用方法是头2
        s.setHeader("content-disposition", cd);                //必须设置2
                
        FileInputStream f = new FileInputStream(p);
        ServletOutputStream outputStream = s.getOutputStream();
        int len = 0;
        while ((len = f.read(b)) != -1) {
            outputStream.write(b, 0, len);
        }
    }
    
=================================================

servlet异常处理:似乎只能在每个方法内try/catch

=================================================

servlet ajax:
        String s = JSON.toJSONString(data);
        response.setCharacterEncoding("utf-8");
        response.getWriter().write(s);


===============================================================================

struts2上传: