springmvc中常见的简单的文件上传和下载

5 篇文章 0 订阅
3 篇文章 0 订阅

一.文件上传:

这里只使用不用导包的part方法

1:上传单个文件

@PostMapping("uploadByPart2")
    public String uploadByPart2(HttpServletRequest request) throws IOException, ServletException {
        //找到要存放文件的绝对路径
        String realPath = request.getServletContext().getRealPath("/uploadExer/");
        //看文件夹是否存在
        File file = new File(realPath);
        while(!file.exists()){
            file.mkdirs();
        }
        //创建名称,防止重名
        Part images = request.getPart("image");
        String extension = StringUtils.getFilenameExtension(images.getSubmittedFileName());
        long nanoTime = System.nanoTime();
        images.write(realPath+nanoTime+"."+extension);
        //此处的success是我自己创建的success.jsp页面
        return "success";
    }

2:上传多个文件

//用Part方式处理多文件上传
    @PostMapping("uploadByparts")
    public String uploadByparts(HttpServletRequest request) throws IOException, ServletException {
        Collection<Part> parts = request.getParts();
        String realPath = request.getServletContext().getRealPath("/uploadFiles/");
        File file = new File(realPath);
        while(!file.exists()){
            file.mkdirs();
        }
        parts.forEach(s->{
            String filenameExtension = StringUtils.getFilenameExtension(s.getSubmittedFileName());
            long nanoTime = System.nanoTime();
            try {
                s.write(realPath+nanoTime+"."+filenameExtension);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        return "success";
    }

二:下载

@GetMapping("download")
    public ResponseEntity<byte[]> fun1(HttpServletRequest request) throws IOException {
        String realPath = request.getServletContext().getRealPath("/download/第一课.docx");
        FileInputStream in = new FileInputStream(realPath);
        byte[] buffer = new byte[in.available()];
        in.read(buffer);
        //调起浏览器下载功能(通过响应头,告知浏览器这个请求是一个下载)
        HttpHeaders httpHeaders = new HttpHeaders();
        //设置下载功能
        httpHeaders.setContentDispositionFormData("attachment","第一课.docx");
        ResponseEntity<byte[]> responseEntity=new ResponseEntity<>(buffer,httpHeaders, HttpStatus.OK);
        return responseEntity;
    }

当然了,其实web.xml配置以及springmvc.xml配置也很重要,不可缺少

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-config.xml</param-value>
        </init-param>
        <multipart-config/>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.javasm.springmvc.controller"/>
    <mvc:annotation-driven/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
<!--    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>-->
</beans>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值