两个springMVC的controller之间传递zip文件(非常感谢张晨同学提供的资料)

来源:互联网 发布:大圣娶亲知乎 编辑:程序博客网 时间:2024/04/28 22:33
<form id="myform2" action="<%=basePath%>cbjc/cbjccbksend.do" method="post"
                    class="kaobei-center-center">
                    <span>比对库名称</span>
                    <input type="text" name="name" id="name" value="${name }"/>
                    <br />
                    <span>比对库编号</span>
                    <input type="text" name="id" id="id" value="${num }"/>
                    <br />
                    <span>比对库描述</span>
                    <br />
                    <textarea name="description" id="description" value="${description }"></textarea>
                    <br />
                    <input type="file" id="singlefile" name="singlefile" accept="application/x-zip-compressed" value=""/>

                </form>



@RequestMapping(value = "cbjccbksend.do", method = { RequestMethod.POST,RequestMethod.GET })
    public String cbjccbksend(HttpServletRequest request) {
        String id=request.getParameter("id");
        String name=request.getParameter("name");
        String description=request.getParameter("description");
        String singlefile=request.getParameter("singlefile");
        String path_locals = request.getSession().getServletContext()
        .getRealPath("");
        String path = path_locals + "/pages/cbkupload/";
        File file=new File(path+singlefile);
        PostMethod postMethod = new PostMethod(yunlongurl);
        try {
            System.out.println("我来了!++++");
            Part[] parts = { new FilePart("file", file),
                    new StringPart("fileName", "file"),
                    new StringPart("id", id),
                    new StringPart("name", name, "UTF-8"),
                    new StringPart("singlefile", name, "UTF-8"),
                    new StringPart("description", description, "UTF-8")};
            MultipartRequestEntity mre = new MultipartRequestEntity(parts,
                    postMethod.getParams());
            postMethod.setRequestEntity(mre);
            HttpClient client = new HttpClient();
            client.getHttpConnectionManager().getParams().setConnectionTimeout(
                    50000);
            // 请求
            System.out.println("我走了!++++");
            int status = client.executeMethod(postMethod);
            // 结束时间
            if (status == HttpStatus.SC_OK) {
                byte[] responseBody = postMethod.getResponseBody();
                String responseStr = new String(responseBody, "utf-8");
                // ********业务处理
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            postMethod.releaseConnection();
        }
        return null;
    }



@RequestMapping(value = "/acceptFile.do", method = { RequestMethod.POST,
            RequestMethod.GET })
    public String missionreceive(
            @RequestParam(value = "file", required = false) MultipartFile file,
            HttpServletRequest request) {
        String name3=(String) request.getAttribute("singlefile");
        String name2=file.getName();
        String name1=file.getOriginalFilename();
        System.out.println(name2+"==="+name1+"=="+name3);
        String path_locals = request.getSession().getServletContext()
                .getRealPath("");
        String name=request.getParameter("singlefile");
        String path = path_locals + "/pages/cbkupload/" + "111" + "/"+file.getName();
        File f=new File(path);
        if(!f.exists()){
            f.mkdirs();
        }
        if (file == null) {
            return "没收接收到文件";
        }
        System.out.println(file);
        try {
            OutputStream out = new FileOutputStream(f);
            // 保存图片 FileCopyUtils是Spring自带的工具类
            FileCopyUtils.copy(file.getInputStream(), out);
        } catch (Exception e) {
            e.printStackTrace();
            return "报错咯";
        }
        return "上传成功";
    }


参考资料:

http://www.zhangc.cn/post/78.html

0 0