文件的上传下载

package com.zt.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

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


@Controller
@RequestMapping(value = "/uploadfile", method = RequestMethod.POST)
public class UploadFileController {

    @RequestMapping("/uploadfile")
    //这个代码会在本地生成一个文件(单个文件上传到本地) 单文件上传
    public String testUploadRequest(HttpServletRequest request, MultipartHttpServletRequest multiReq) {
        //获取文件上传的路径
        String Uploadfilepath = multiReq.getFile("file1").getOriginalFilename();
        System.out.println("Uploadfilepath==" + Uploadfilepath);
        //截取上传的文件名
        String filename = Uploadfilepath.substring(Uploadfilepath.lastIndexOf('\\') + 1, Uploadfilepath.indexOf('.'));
        System.out.println("filename==" + filename);
        //截取上传文件的后缀
        String fileSuffix = Uploadfilepath.substring(Uploadfilepath.indexOf('.') + 1, Uploadfilepath.length());
        System.out.println("fileSuffix==" + fileSuffix);

        FileOutputStream fos = null;//文件输入流
        FileInputStream fis = null;

        try {
            fis = (FileInputStream) multiReq.getFile("file1").getInputStream();  //类型转换 字符输入流转换为文件输入流
            fos = new FileOutputStream(new File("E:\\" + filename + ".") + fileSuffix);
            byte[] temp = new byte[1024];
            int len = fis.read(temp);
            while (len != -1) {
                fos.write(temp, 0, temp.length);
                fos.flush();
                len = fis.read(temp);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
        System.out.println("成功!!!");
        String msg = "成功";
        return "upload";
    }

    @RequestMapping("/uploadfiles")
//    @ResponseBody   多文件上传
    public String testUploadRequests(HttpServletRequest request) {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file1");
        MultipartFile file = null;
        BufferedOutputStream stream = null;//缓冲流
        BufferedInputStream inputStream=null;
        for (int i = 0; i < files.size(); i++) {
            file = files.get(i);
            if (!file.isEmpty()) {
                try {
                    String filepath = file.getOriginalFilename();
                    System.out.println("文件路径==" + filepath);
                    //截取上传的文件名
                    String uploadfilename = filepath.substring(filepath.lastIndexOf('\\') + 1, filepath.indexOf('.'));
                    System.out.println("文件名==" + uploadfilename);
                    //获取文件的后缀名
                    String filesuffix = filepath.substring(filepath.indexOf('.') + 1, filepath.length());
                    System.out.println("后缀名==" + filesuffix);

                    stream = new BufferedOutputStream(new FileOutputStream(new File("E:\\" + uploadfilename + "." + filesuffix)));
                    byte[] bytes = new byte[1024];
                    int len=inputStream.read(bytes);
                    if(len!=-1) {
                        stream.write(bytes, 0, bytes.length);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (stream != null) {
                        try {
                            stream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            } else {
                System.out.println("上传文件为空");
            }
        }
        System.out.println("文件上传成功");
        return "upload";
    }


    //文件下载  
    @RequestMapping(value = "/filedown" , method = RequestMethod.GET)
    public void Testdown(HttpServletResponse res) {
        Date date=new Date();
        System.out.println("date=="+date);
        String filename = "";
        res.setHeader("content-type","application/octet-stream");
        res.setContentType("application/octet-stream");
        res.setHeader("Content-Disposition","attachment;filename="+filename);
        byte[] byts = new byte[1024];
        BufferedInputStream bis=null;
        OutputStream os = null;
        try {
            os = res.getOutputStream();
            bis = new BufferedInputStream(new FileInputStream(new File("E:\\down\\111.png"+filename)));//此处直接写死路径用于下载
            int i=bis.read(byts);
            while (i != -1){
                os.write(byts,0,byts.length);
                os.flush();
                i = bis.read(byts);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}

 

本文参考资料  https://www.cnblogs.com/studyCenter/p/6665171.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值