SpringMVC-文件上传的三种类型及常见问题和解决

03- 文件上传

参考视频:https://www.bilibili.com/video/BV1mE411X7yp?p=197
up主讲的很好,也欢迎去学习,(纯路人滴滴)
以下为纯手敲笔记~还有些不足,呜

  1. form 表单的enctyoe 取值必须是:multipart / form-data;(注意:默认:application / x-www-form-urlencoded ): enctype 是表单请求正文的类型;
  2. method 属性必须是 Post;
  3. 《input type=“ file ” 》;

**原理:**fileUpload:

request. getParameter ( )会失效;依赖坐标:

<dependency>
  <groupId>com.liferay.org.apache.commons.fileupload</groupId>
  <artifactId>com.liferay.org.apache.commons.fileupload</artifactId>
  <version>6.2.0.1</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.10.0</version>
</dependency>

一般方法:

@RequestMapping("/fileUpload")
public String fileUpload(HttpServletRequest req){
    System.out.println("文件上传成功。。。。。。");
    String path = req.getSession().getServletContext().getRealPath("/uploads/");
    File newFile = new File(path);
    if(!newFile.exists()){
        newFile.mkdirs();
    }
    // factory:
    DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(fileItemFactory);//
    // request :
    try {
        List<FileItem> lists = upload.parseRequest(req);
        for (FileItem item : lists) {
            if(item.isFormField()){
                // 普通表单项
            }else{
                // the file:
                // get the name;
                String name = UUID.randomUUID().toString().replace("-","")+"_"+item.getName();
                item.write(new File(path,name));
                // 删除临时文件:
                item.delete();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "success";
}

但是遇到了问题,返回list为空,图片上传成功了,但是没有被读取呜呜;


SpringMVC的方式:

bean配置:

<!-- 文件解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="10485760"/><!-- 10MB -->
</bean>

index.jsp:

<h1>file uploading:fileUploadByMVC</h1>
<form action="response/fileUploadByMVC" method="post" enctype="multipart/form-data">
    <label for="fileUpload">文件上传:</label><input name="upload" type="file" id="fileUpload1" value="click-to-upload"/>
    <button type="submit">upload</button>
</form>

source code:

@RequestMapping("/fileUploadByMVC")
public String fileUploadByMVC(HttpServletRequest req, MultipartFile upload) throws IOException {
    System.out.println("MVC文件上传成功。。。。。。");
    String path = req.getSession().getServletContext().getRealPath("/uploads/");
    File newFile = new File(path);
    if(!newFile.exists()){
        newFile.mkdirs();
    }
    String post_file = upload.getName();
    String pre_name = UUID.randomUUID().toString().replace("-","")+"_";
    String fileName = pre_name+post_file;
    upload.transferTo(new File(path,fileName));

    return "success";
}

跨服务器:

依赖:

<dependency>
  <groupId>com.sun.jersey</groupId>
  <artifactId>jersey-core</artifactId>
  <version>1.18.6</version>
</dependency>
<dependency>
  <groupId>com.sun.jersey</groupId>
  <artifactId>jersey-client</artifactId>
  <version>1.18.6</version>

遇到问题:404not found:(13条消息) SpringMVC跨服务器上传文件出现returned a response status of 400 OR 403 OR 404 OR 409_chpllp的博客-CSDN博客

405错误:多半是tomcat下的配置,不允许,在conf路径下的web.xml中找到default的servlet 进行配置就可以啦;

<init-param>
            <param-name>readonly</param-name>
            <param-value>false</param-value>
</init-param>
<!-- tomcat-config-web.xml -->

发送方:

源码:client 的使用;

    @RequestMapping("/fileUploadByClient")
    public String fileUploadByClient(MultipartFile upload) throws IOException {
        System.out.println("Client文件上传成功。。。。。。");
        String path = "http://localhost:9090/uploads/";
        String post_file = upload.getOriginalFilename();// the initial name;
        String pre_name = UUID.randomUUID().toString().replace("-","")+"_";
        String fileName = pre_name+post_file;
        // get the client:
        Client client = Client.create();
        WebResource webResource = client.resource(path + fileName);
        webResource.put(upload.getBytes());
//        upload.transferTo(new File(path,fileName));

        return "success";
    }

在这里插入图片描述


目的服务器的一些注意配置:

在这里插入图片描述

在这里插入图片描述


over!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

J.CH.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值