springmvc文件下载和上传

下载模块:

  @RequestMapping("/download")
    public static void download(HttpServletRequest request,  
              HttpServletResponse response, String storeName, String contentType
               ) throws Exception {  

            request.setCharacterEncoding("UTF-8");  
            BufferedInputStream bis = null;  
            BufferedOutputStream bos = null;  

            //获取项目根目录
//          String ctxPath = request.getSession().getServletContext()  
//              .getRealPath("");  

            //获取下载文件露肩
            String downLoadPath = "F:\\gp\\李霞\\Activity.doc";  

            //获取文件的长度
            long fileLength = new File(downLoadPath).length();  
            String filename="Activity学习总结.doc";
            //设置文件输出类型
            response.setContentType("application/octet-stream");  
            response.setHeader("Content-disposition", "attachment; filename="  
                + new String(filename.getBytes("utf-8"), "ISO8859-1")); 
            //设置输出长度
            response.setHeader("Content-Length", String.valueOf(fileLength));  
            //获取输入流
            bis = new BufferedInputStream(new FileInputStream(downLoadPath));  
            //输出流
            bos = new BufferedOutputStream(response.getOutputStream());  
            byte[] buff = new byte[2048];  
            int bytesRead;  
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  
              bos.write(buff, 0, bytesRead);  
            }  
            //关闭流
            bis.close();  
            bos.close();  
          }  

上传模块:
前台页面参考

https://blog.csdn.net/qq_36345181/article/details/79929650

后台
Controller层

    @RequestMapping("/file/upload")
    @ResponseBody
    public R doUploadFile(@ModelAttribute GPFile gpFile, String fileName) {

        if (!gpFile.getUploadFile().isEmpty()) {
            try {
                String username = (String) SessionUtiles.getLoginName();
                String filepath = "F:\\gp\\" + username;
                // 这里将上传得到的文件保存至目录
                FileUtils.copyInputStreamToFile(gpFile.getUploadFile().getInputStream(),
                        new File(filepath, gpFile.getUploadFile().getOriginalFilename()));
                // 上传者的信息
                User user = loginService.findBypwd(username);
                gpFile.setUserId(user.getId());
                gpFile.setFileAddress(filepath);
                commonService.insert(gpFile);
            } catch (IOException e) {
                e.printStackTrace();
                return R.error("上传失败");
            }
        }
        return R.ok();
    }

Bean层

public class GPFile implements Serializable
{
    private Long id;

    private String fileName;

    private String fileAddress;

    private String fileType;

    private Long userId;

    private Date gmtCreate;

    private Date gmtModified;

    private MultipartFile uploadFile;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值