spring中处理文件下载

我们只需要在控制器这样写就可以了:

有什么不懂的可以看一下代码中的注释:

    @RequestMapping("fileDownLoad")
    public void fileDownLoad(String filename, String fileType, HttpServletRequest request, HttpServletResponse response) throws IOException {
//        得到当前下载图片的路径
        String realPath = request.getServletContext().getRealPath("/img");
//        得到当前下载的文件名
        File file=new File(realPath+"/"+filename);
//        从服务器读取相应的图片资源
        InputStream inputStream=new FileInputStream(file);
//        ------------------------------------------
//        设置响应的格式:(下载文件的三大步骤)
//        设置下载文件的长度
        response.setContentLength((int) file.length());
//      文件格式
        response.setContentType(fileType);
        response.setHeader("Content-Disposition","attachment;filename="+filename);
//     ----------------------------------------------------
//        将文件下载到本地
        OutputStream outputStream=response.getOutputStream();
        IOUtils.copy(inputStream,outputStream);
        outputStream.close();
        inputStream.close();
    }

控制器的全部方法:

package com.bjsxt.controller;

import com.bjsxt.pojo.Student;
import com.bjsxt.service.StudentService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.UUID;

@Controller
public class MyController {

    @Autowired
    StudentService studentService;

    @RequestMapping("/demo")
    public String demo(Student student,MultipartFile fil, HttpServletRequest req) throws IOException {
//      设定文件的存储路径,如果文件不存在就创建文件
        File file=new File(req.getServletContext().getRealPath("/img"));
        if(!file.exists()){
            file.mkdir();
        }
// getName是表单的name,getOriginalFilename是获得文件全名  getSize是获得文件大小  getContentType是获得文件路径
        System.out.println(fil.getName()+"---"+fil.getOriginalFilename()+"--"+fil.getSize()+"-"+fil.getContentType());
//       因为重复的文件名会替换之前的文件名,所以使用UUID,可以防止文件名重复
        String uuid = UUID.randomUUID().toString();

        String jgp = fil.getOriginalFilename().substring(fil.getOriginalFilename().lastIndexOf("."));
//        新的文件名   uuid.jgp
        String fileName=uuid+jgp;
//        文件转换,将文件存储转换到file里,名字是fileName
        fil.transferTo(new File(file,fileName));
        student.setId(0);
        student.setFilename(fileName);
        student.setFileType(fil.getContentType());
        studentService.addStudent(student);
        return "redirect:success.jsp";
    }

    @RequestMapping("selAll")
    @ResponseBody
    public List selAll(){
        List<Student> students = studentService.selAll();
        return students;
    }

    @RequestMapping("fileDownLoad")
    public void fileDownLoad(String filename, String fileType, HttpServletRequest request, HttpServletResponse response) throws IOException {
//        得到当前下载图片的路径
        String realPath = request.getServletContext().getRealPath("/img");
//        得到当前下载的文件名
        File file=new File(realPath+"/"+filename);
//        从服务器读取相应的图片资源
        InputStream inputStream=new FileInputStream(file);
//        ------------------------------------------
//        设置响应的格式:(下载文件的三大步骤)
//        设置下载文件的长度
        response.setContentLength((int) file.length());
//      文件格式
        response.setContentType(fileType);
        response.setHeader("Content-Disposition","attachment;filename="+filename);
//     ----------------------------------------------------
//        将文件下载到本地
        OutputStream outputStream=response.getOutputStream();
        IOUtils.copy(inputStream,outputStream);
        outputStream.close();
        inputStream.close();
    }

    @RequestMapping("filter")
    public String filter(String name){
        System.out.println("name = " + name);
        return "add.jsp";
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值