文件的上传与下载之方式3:Spring MVC

方式三:spring mvc

首先依旧是导包;不过在这里建的是maven项目,因此用的是依赖

    <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
    </dependency>
    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
                <version>4.3.10.RELEASE</version>
    </dependency>

然后则是前端的页面

<body>
        <form action="file/upload.do" method="post" enctype="multipart/form-data">
            上传者:<input type="text" name="name" /><br/>
            上传的文件:<input type="file" name="file" multiple="multiple"/><br/>
            <input  type="submit" value="上传" />
        </form>
  </body>

<!--下载-->
<a href="file/download.do">点击下载</a>

java代码部分

package com.zdl;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.HeaderParam;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("file")
public class TestFile {
    //上传部分
    @RequestMapping(path ="upload.do")
    public String upload(HttpServletRequest request,@RequestParam(value="file") MultipartFile [] mfile,String name){

        for (MultipartFile multipartFile : mfile) {

            String path = request.getServletContext().getRealPath("upload")+"\\"+multipartFile.getOriginalFilename();

            System.out.println(name+"--"+path);
            File file = new File(path);
            try {
                multipartFile.transferTo(file);
                System.out.println("上传成功:"+multipartFile.getOriginalFilename());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return "success";
    }

    //下载部分

    @RequestMapping(path="download.do")
    public ResponseEntity<byte[]> download(@RequestHeader("User-Agent") String agent) throws Exception{

        File file = new File("C:\\Users\\Administrator\\Desktop\\like.png");
        //设置响应的说明和额外的参数
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

        //由注解@RequestHeader("User-Agent")获取到浏览器的类型来解决中文乱码的问题(因为浏览器的兼容性不同)
        String filename = "";
        if (agent.toUpperCase().contains("CHROME")) {
            filename = new String(file.getName().getBytes("utf-8"),"iso-8859-1");
        }else if (agent.toUpperCase().contains("MSIE")) {
            filename = new String(file.getName().getBytes("iso-8859-1"),"utf-8");
        } 


        headers.setContentDispositionFormData("attachment", filename);

        //将文件转成byte数组
        byte[] by = FileUtils.readFileToByteArray(file);
        //三个参数分别是一个文件的byte数组,头部信息以及响应码(201)
        ResponseEntity<byte[]> rn = new ResponseEntity<byte[]>(by,headers, HttpStatus.CREATED);
        return rn;
    }
}

springmvc.xml配置文件中上传必不可少的一部分

<context:component-scan base-package="com.zdl" />

<!-- 文件上传解析器:MultipartResolver -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8" />
        <property name="maxUploadSize" value="10485760" />
    </bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值