栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

文件上传

    文件上传原理:

        待补充....    

    1.传统的文件上传:
        commons-fileupload.jar
        commons-io.jar传统文件上传需要的jar包
     
        表单格式:必须有enctype,表单input标签的name属性需要为upload或者其它也可,但必须有name属性
        
选择文件
@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 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配置中添加: 表单格式:必须有enctype,表单input标签的name属性需要与方法参数的相同即可 spring mvc上传文件
选择文件
//测试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配置 readonly false -------------------------------------------------- 编码: 跨服务器上传文件
选择文件
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需要配置 readonly false 409错误: returned a response status of 409 Conflict解决方法:在工作空间中找到对应的工程, 在target目录下建立相应的文件夹接受文件即可

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/345320.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号