服务器文件上传:传统、SpringMvc和跨服(个人笔记)

文件上传

    文件上传原理:

        待补充....    

    1.传统的文件上传:
        commons-fileupload.jar
        commons-io.jar传统文件上传需要的jar包
     
        表单格式:必须有enctype,表单input标签的name属性需要为upload或者其它也可,但必须有name属性
        <form action="user/testUpload" method="post" enctype="multipart/form-data">
            选择文件<input type="file" name="upload"><br>
            <input type="submit" value="上传">
        </form>

        @RequestMapping(path = "/testUpload")
        public String testUpload(HttpServletRequest request) throws Exception {
            System.out.println("上传文件...");
            //使用fileupload组件完成文件上传
            //上传的位置
            String path = request.getSession().getServletContext().getRealPath("/uploads/");
            System.out.println("路径" + path);
            File file = new File(path);
            if(!file.exists()){
                boolean mkdir = file.mkdir();
            }

            //解析request对象获取上传文件项
            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            //解析request
            List<FileItem> items = upload.parseRequest(request);

            for(FileItem item : items){
                //说明不是普通表单项
                if(!item.isFormField()){
                    //说明是文件项,获取文件的名称
                    String filename = item.getName();
                    String uuid = UUID.randomUUID().toString().replace("-","");
                    filename = uuid + "_" + filename;
                    //完成文件上传
                    item.write(new File(path,filename));
                    //删除临时文件,一旦上传文件大小大于10kb就会产生临时文件
                    item.delete();
                }
            }
            return "success";
        }
    2.spring mvc文件上传
        spring.xml配置中添加:
            <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
                <property name="maxInMemorySize" value="10485760"/>
            </bean>
        表单格式:必须有enctype,表单input标签的name属性需要与方法参数的相同即可
            <h4>spring mvc上传文件</h4>
            <form action="user/testSpringUpload" method="post" enctype="multipart/form-data">
                选择文件<input type="file" name="px"><br>
                <input type="submit" value="上传">
            </form>
        //测试spring上传文件
        @RequestMapping(path = "testSpringUpload")
        public String testSpringUpload(HttpServletRequest request, @RequestParam("px") MultipartFile upload) throws Exception {
            System.out.println("spring mvc上传文件...");
            //使用fileupload组件完成文件上传
            //上传的位置
            String path = request.getSession().getServletContext().getRealPath("/uploads/");
            System.out.println("路径" + path);
            File file = new File(path);
            if(!file.exists()){
                file.mkdirs();
            }

            //说明是文件项,获取文件的名称
            String filename = upload.getOriginalFilename();
            String uuid = UUID.randomUUID().toString().replace("-","");
            filename = uuid + "_" + filename;
            upload.transferTo(new File(path,filename));
            System.out.println("spring mvc文件上传结束!");
            return "success";
        }

    3.跨服务器文件上传,依赖jar包:
        jersey-client.jar
        jersey-core.jar

        跨服务器文件上传的注意事项:
            存储服务器如果是web项目,要发布带exploded的工程。

        tomcat的conf/web.xml配置
            <init-param>
                <param-name>readonly</param-name>
                <param-value>false</param-value>
            </init-param>
         --------------------------------------------------
        编码:
        <h4>跨服务器上传文件</h4>
        <form action="user/testOverServer" method="post" enctype="multipart/form-data">
            选择文件<input type="file" name="ur"><br>
            <input type="submit" value="上传">
        </form>

        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="maxInMemorySize" value="10485760"/>
        </bean>

        3.测试跨服务器上传文件
        @RequestMapping(path = "/testOverServer")
        public String testOverServer(@RequestParam("ur") MultipartFile upload) throws Exception {
            System.out.println("跨服务器上传文件...");

            String path = "http://localhost:9090/server/uploads/";
            System.out.println("路径" + path);

            //说明是文件项,获取文件的名称
            String filename = upload.getOriginalFilename();
            String uuid = UUID.randomUUID().toString().replace("-","");
            filename = uuid + "_" + filename;

            //创建客户端的对象
            Client client = Client.create();
            //和目标服务器进行连接
            WebResource resource = client.resource(path + filename);
            //上传文件
            resource.put(upload.getBytes());

            System.out.println("跨服务器文件上传结束!");
            return "success";
        }


        跨站文件上传总结:真——他——*——的——*——蛋!
            springmvc-config.xml在做文件上传配置时,文件上传的解析器id是固定的,不能起别的名    
        称,否则无法实现请求参数的绑定(不光是文件,其它字段也将无法绑定)

        文件上传时出现如下错误:
            404错误:
                路径错误
            405错误:
                Method Not Allowed错误,tomcat需要配置
                <init-param>
                    <param-name>readonly</param-name>
                    <param-value>false</param-value>
                </init-param>
            409错误:
                returned a response status of 409 Conflict解决方法:在工作空间中找到对应的工程,
                在target目录下建立相应的文件夹接受文件即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值