SpringMVC文件上传

SpringMVC文件上传的三种方式:

jsp代码:

 <h1>springmvc跨服务器文件上传</h1>
  <form action="testFileupload3" name="fileupload" enctype="multipart/form-data" method="post">
    <input type="file" name="upload"/>
    <input type="submit" value="提交"/>
  </form>
  <br/> <br>
  <h1>springmvc文件上传</h1>
  <form action="testFileupload2" name="fileupload" enctype="multipart/form-data" method="post">
    <input type="file" name="upload"/>
    <input type="submit" value="提交"/>
  </form>
  <br/> <br>
  <h1>文件上传</h1>
  <form action="testFileupload" name="fileupload" enctype="multipart/form-data" method="post">
    <input type="file" name="upload"/>
    <input type="submit" value="提交"/>
  </form>
  <br/> <br>

java的Controller代码:

//    springmvc跨服务器的文件上传
    @RequestMapping("/testFileupload3")
    public String testFileupload3( MultipartFile upload) throws Exception {
        String originalFilename = upload.getOriginalFilename();
        String uuid= UUID.randomUUID().toString().replace("-","");
        String filename= uuid+"_"+originalFilename;

        String path="http://localhost:9090/uploads/";//需要手动在专门放文件的服务器下创建个uploads文件夹。对应path这个目录。
        Client client = Client.create();
        WebResource resource = client.resource(path + filename);
        resource.put(upload.getBytes());

        return SUC;
    }

//    springmvc方式的文件上传
    @RequestMapping("/testFileupload2")
    public String testFileupload2(HttpServletRequest request, MultipartFile upload) throws Exception {
        String realPath = request.getSession().getServletContext().getRealPath("/uploads/");
        File file =new File(realPath);
        if (!file.exists())
        {
            file.mkdirs();
        }

        String originalFilename = upload.getOriginalFilename();
        String uuid= UUID.randomUUID().toString().replace("-","");
        String filename= uuid+"_"+originalFilename;
        upload.transferTo(new File(realPath,filename));
        return SUC;
    }

//    传统方式的文件上传
    @RequestMapping("/testFileupload")
    public String testFileupload(HttpServletRequest request) throws Exception {
        String realPath = request.getSession().getServletContext().getRealPath("/uploads/");
        File file =new File(realPath);
        if (!file.exists())
        {
            file.mkdirs();
        }

        DiskFileItemFactory factory=new DiskFileItemFactory();
        ServletFileUpload upload=new ServletFileUpload(factory);
        List<FileItem> fileItems = upload.parseRequest(request);
        for (FileItem item:fileItems)
        {
            if (item.isFormField())
            {
                System.out.println("普通而已");
            }
            else
            {
                String itemName = item.getName();
                item.write(new File(realPath,itemName));
                item.delete();
            }
        }

        return SUC;
    }

注意:要新建一个module然后部署到tomcat上作为第二个专门存放文件的服务器,这时,要改专门存放文件的服务器的端口:

这是默认的端口:

遇到的坑:在跨服务器上传的时候,专门存放文件的那个服务器需要做一个配置,如下:

<servlet>
        <servlet-name>default</servlet-name>
        <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
        <init-param>
            <param-name>readonly</param-name>
            <param-value>false</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

为这个servlet需要在maven引入依赖如下:

        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>catalina</artifactId>
            <version>6.0.53</version>
        </dependency>

如果没有那个default servlet的“readonly”为false的配置会出现如下错误:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值