• springMVC上传和下载文件


      //springMVC只能上传到本地,不同电脑之间参考FTP上传

    @RequestMapping(value ="uploadFileStore.do", method = RequestMethod.POST)
    public void uploadFileStore(HttpServletResponse response, FileStore fileStore, @RequestParam("wj") MultipartFile wj, HttpServletRequest request) throws IOException {
      if (wj == null) {
        ResponseUtils.textWriterOfFail(response, "无法获取文件");
        return;
      }
      //上传地址,我是保存在properties中的(方便改),这里取一下
      String path = "";
      try {
        try {
          Properties properties = PropertiesUtil.readProperties("sys.properties");
          path = properties.getProperty("fs.upload");
        } catch (IOException e) {
          e.printStackTrace();
        }
    
        //保存一下文件信息
        fileStore.setFileUrl(Upload.upload(path, wj));
        fileStoreService.insert(fileStore);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }



     

      //下载文件

    @RequestMapping("downloadFileStore")
    public void downloadFsFileStore(HttpServletResponse response, HttpServletRequest request, String fsId) throws IOException {
      try {
        FileStore fileStore = fileStoreService.queryById(fsId);
        String filename = fileStore.getFileUrl().substring(fileStore.getFileUrl().lastIndexOf("/") + 1);
        String path = fileStore.getFileUrl();
        Upload.download(filename, path, request, response);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    
    

     

     

     

     上面调用的Upload类

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.URLEncoder;
    import java.util.Calendar;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.multipart.MultipartFile;
    
    import com.flatform.frame.core.utils.UUIDUtils;
    
    public class UploadIN {
      public static String upload(String uploadpath,//path 上传路径 
      MultipartFile multipartFile){
        // 获取文件名
        String filename = multipartFile.getOriginalFilename();
        //重新来一个(这样就不用特地去截取文件类型了)
        filename="("+UUIDUtils.create().substring(0, 8)+")"+filename;
        // 完整路径(这一步可以无视,这里方便查找用)
        Calendar calendar=Calendar.getInstance();
        uploadpath=uploadpath+"/"+calendar.get(Calendar.YEAR)+"/"+
        (calendar.get(Calendar.MONTH)+1)+"/"+calendar.get(Calendar.DATE);
        File targetFile = new File(uploadpath, filename);
        // 判断文件夹是否已经存在,如果不存在重新建
        if (!targetFile.exists()) {
          targetFile.mkdirs();
        }
        // 转存文件
        try {
          multipartFile.transferTo(targetFile);
          return uploadpath+ "/" + filename;
        } catch (Exception e) {
          throw new RuntimeException("上传失败");
        }
      }
      public static void download(String fileName, String filePath,   HttpServletRequest request, HttpServletResponse response)    throws Exception {      //设置响应头和客户端保存文件名      response.setCharacterEncoding("utf-8");      response.setContentType("multipart/form-data");     // URLEncoder.encode(fileName, "UTF-8")改一下标题的编码格式,不然可能乱码      response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));      try {         File file = new File(filePath);          //打开本地文件流          InputStream inputStream = new FileInputStream(file);          //激活下载操作          OutputStream os = response.getOutputStream();          //循环写入输出流          byte[] b = new byte[2048];          int length;          while ((length = inputStream.read(b)) > 0) {              os.write(b, 0, length);          }                  // 这里主要关闭          os.close();          inputStream.close();      } catch (Exception e){          throw e;      }   } }
  • 相关阅读:
    NEC的学习笔记
    MVC过滤器中获取实体类属性值
    Facebook Hack 语言 简介
    6.格式化输出(转)
    ProcessBuilder 和 Runtime(转)
    JAVA I/O使用方法(转)
    java中输出流OutputStream 类应用实例(转)
    InputStream、OutputStream、String的相互转换(转)
    海茶3 らぶデス3 入门经典教程
    天将降大任于斯人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行拂乱其所为,所以动心忍性,增益其所不能
  • 原文地址:https://www.cnblogs.com/IceBlueBrother/p/8423138.html
Copyright © 2020-2023  润新知