• springboot 文件上传和下载


    文件的上传和下载
    1、文件上传

    html页面代码如下

    <form method="post" action="/file/upload1" enctype="multipart/form-data">
    <p>
    文件:<input type="file" name="file">
    <input type="submit" value="上传">
    </p>
    </form>
    Controller代码,
    @RequestMapping(“/upload1”)
    @ResponseBody
    public String upload1(@RequestParam(“file”)MultipartFile file,HttpServletRequest request) throws IOException {
    //获取文件名
    String filename=file.getOriginalFilename();
    //文件上传后路径
    String path=”E://images//“;
    File file1=new File(path+filename);
    //如果文件不存在,则创建新的文件夹
    if(!file1.getParentFile().exists()){
    file1.getParentFile().mkdirs();
    }
    byte[] bytes=file.getBytes(); //获得文件字节
    //将filename文件写入到path路径下
    FileOutputStream fos=new FileOutputStream(file1);
    fos.write(bytes); //将字节写入
    fos.flush();
    fos.close();
    return “ok”;
    }
    判断文件的另一个方法,将
    File file1=new File(path+filename);
    //如果文件不存在,则创建新的文件夹
    if(!file1.getParentFile().exists()){
    file1.getParentFile().mkdirs();
    }
    FileOutputStream fos=new FileOutputStream(file1);
    替换成
    File file1=new File(path);
    //如果文件不存在,则创建新的文件夹
    if(!file1.exists()){
    file1.mkdirs();
    }
    FileOutputStream fos=new FileOutputStream(path+filename);

    2、文件上传(第二中方法)
    1. @RequestMapping("/upload1")
    2. @ResponseBody
    3. public String upload1(@RequestParam("file")MultipartFile file,HttpServletRequest request) throws IOException {
    4. //获取文件名
    5. String filename=file.getOriginalFilename();
    6. //文件上传后路径
    7. String path="E://images//";
    8. File file1=new File(path+filename);
    9. //如果文件不存在,则创建新的文件夹
    10. if(!file1.getParentFile().exists()){
    11. file1.getParentFile().mkdirs();
    12. }
    13. //上传的文件存放的位置
    14. file.transferTo(file1);
    15. return "ok";
    16. }
    3、文件下载代码

    //文件下载
    @RequestMapping(“/download”)
    @ResponseBody
    public String download(HttpServletResponse response) throws IOException {
    //要下载的文件路径
    String filepath=”E://images//“;
    //要下载的文件名称
    String filename=”1.jpg”;
    File file=new File(filepath,filename);
    //判断文件是否存在
    if(file.exists()){
    response.setContentType(“application/force-download”);//设置强制下载不打开
    response.addHeader(“Content-Disposition”,”attachment;fileName=”+filename);//设置文件名
    byte[]buf=new byte[1024];
    //文件输入了
    FileInputStream fis=null;
    //带缓冲的字节流
    BufferedInputStream bis=null;
    OutputStream os=null;//输出流
    try{
    fis=new FileInputStream(file);
    bis=new BufferedInputStream(fis);
    os=response.getOutputStream();
    int i=bis.read(buf);
    while (i!=-1){
    os.write(buf);
    i=bis.read(buf);
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }finally {
    if(bis!=null){
    bis.close();
    }if(fis!=null){
    fis.close();
    }
    }
    }
    return “download”;
    }

    4、多文件上传代码

    html页面代码

    <form action="/file/uploads" method="post" enctype="multipart/form-data">
    <p>文件1:<input type="file" name="file"></p>
    <p>文件2:<input type="file" name="file"></p>
    <p>文件3:<input type="file" name="file"></p>
    <p> <input type="submit" value="上传"></p>
    </form>
    Controller代码
    @RequestMapping(“/uploads”)
    @ResponseBody
    public String uploads(HttpServletRequest request){
    //获取上传的文件
    List<MultipartFile>files=((MultipartHttpServletRequest)request).getFiles(“file”);
    MultipartFile file=null;
    BufferedOutputStream stream=null;
    for(int i=0;i<files.size();i++){
    file=files.get(i);
    if(!file.isEmpty()){
    try{
    byte[]bytes=file.getBytes();
    stream=new BufferedOutputStream(new FileOutputStream(
    new File(file.getOriginalFilename())));
    stream.write(bytes);
    stream.close();
    } catch (IOException e) {
    return “failure,”+i+”=>”+e.getMessage();
    }
    }else{
    return “failure,is null”;
    }
    }
    return “ok”;
    }
    总结:以上方法都亲测有效,如果问题请留言。

  • 相关阅读:
    BZOJ1119: [POI2009]SLO
    BZOJ1486: [HNOI2009]最小圈
    BZOJ1098: [POI2007]办公楼biu
    BZOJ2242: [SDOI2011]计算器
    PAT A1023
    SpringCloud之整合Feign
    SpringCloud之整合Feign
    小程序在wxml页面格式化类似的2019-02-16T10:54:47.831000时间
    小程序在wxml页面格式化类似的2019-02-16T10:54:47.831000时间
    Javascript基础之-var,let和const深入解析(二)
  • 原文地址:https://www.cnblogs.com/ming-blogs/p/10288867.html
Copyright © 2020-2023  润新知