菜鸟学SpringMVC之——文件上传及下载

SpringMVC 文件上传及下载

首先form表单需要设置编码格式:enctype=“multipart/form-data”

enctype是设置编码格式,把他设置为多媒体数据上传,之前我们传的是字符串,字符串不需要二进制传输。但是传输的图片等文件可不能用字符流传输,那么怎么让Http知道这个东西要用字节流传输呢。所以把他放到body里,body里面放的都是二进制文件,所以在上传文件时就要设置enctype,设置完后,浏览器在编辑这个文件的时候就会把它放到HTTP的body里。

前端传参的容器有三个:

queryString(公开传数据):里面放的就是url后缀的参数

formdata(私密传数据):表单数据,form表单提交的数据放在这里

上面的两个容器类似,form表单中有个method,为get时就放到querystring里,为post时,放到formdata

body(传文件用,必须以post请求发):里面放的是字节,上面两个里面放的是字符流,如果想放到这个容器里,首先必须是post请求。然后设置enctype为multipart/form-data

先写简单form表单:

<form method="post" enctype="multipart/form-data" action="/upload/file.do">
        <input type="file" name="myFile">
        <input type="submit" value="提交">
</form>

然后写Controller

@RestController//表示返回的数据就是字符串
@RequestMapping("upload")
public class FileUploadController {
    
    

    @Resource(name = "fileService")
    IFileuploadService service;

    @RequestMapping("/file.do")
    @Login
    public String uploadFile(MultipartFile myFile, HttpServletRequest request) {
    
    
       //System.out.println(myFile.getOriginalFilename()); //得到上传的文件名
        String path = request.getRealPath("/static/image");//这就会获得动态变化的路径了,项目发布在哪他的路径就是哪。将文件上传到这个路径下是百害而无一利的,发布在这个路径下就能通过请求直接访问到资源。而如果将路径设置为本地固定地址,就不能通过浏览器访问到了
        service.saveFile(myFile,path);
        return "成功";
    }
        //文件下载(简写)
    @RequestMapping("/down.do")
    public void downloadFile(String path, HttpServletResponse response) throws IOException {
    
    
        FileInputStream fis = new FileInputStream(WebConfig.FILE_PATH + path);//我这里从本地路径下下载的
        byte[] b = new byte[fis.available()];
        fis.read(b);
        response.addHeader("Content-Disposition", "attachment;filename=down"+path);
        response.getOutputStream().write(b);

    }
}

service层

@Service("fileService")
public class FileUploadService implements IFileuploadService {
    
    

    @Override
    public boolean saveFile(MultipartFile file, String path) {
    
    

        String tempPath = path + "/" + System.currentTimeMillis() + file.getOriginalFilename();//加时间戳是为了防止文件重名
        //这个路径大有学问,不能将文件上传路径设置为本地路径,也应该设置为项目发布路径,这是百害而无一利的。所以最后应该用一个服务器去接受文件。

        File f = new File(tempPath);

        try {
    
    
            file.transferTo(f);//把文件放到某路径下
            return true;
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
        return false;
    }
}

配置文件

SpringMVC给我们的视图解析器默认解析字符串是没有问题的,但是他不能解析文件,所以需要SpringMVC有解析文件的能力

    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--上传文件的最大大小,单位为字节 -->
        <property name="maxUploadSize" value="17367648787"></property>
        <!--还有一个属性:maxUploadPerFile,表示多文件上传时,规定每一个文件大小限制-->

        <!-- 上传文件的编码 -->
        <property name="defaultEncoding" value="UTF-8"></property>
    </bean>

还需要导入相关依赖

    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.2</version>
    </dependency>

Guess you like

Origin blog.csdn.net/ysf15609260848/article/details/107140608