zoukankan      html  css  js  c++  java
  • 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”;
    }
    总结:以上方法都亲测有效,如果问题请留言。

  • 相关阅读:
    django大全
    centos 下安装python3.6.2
    爬虫基础知识与简单爬虫实现
    HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)
    ZOJ5833 Tournament(递归打表)
    ZOJ4067 Books(贪心)
    ZOJ4062 Plants vs. Zombies(二分+贪心)
    ZOJ4060 Flippy Sequence(思维题)
    洛谷P2568 GCD(线性筛法)
    2018.11.6刷题记录
  • 原文地址:https://www.cnblogs.com/ming-blogs/p/10288867.html
Copyright © 2011-2022 走看看