Chinaunix首页 | 论坛 | 博客
  • 博客访问: 603794
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2022-02-28 16:25:49


点击(此处)折叠或打开


  1. package com.fh.util;

  2. import java.io.BufferedInputStream;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.net.MalformedURLException;
  9. import java.net.URL;

  10. import org.apache.commons.io.FileUtils;
  11. import org.springframework.web.multipart.MultipartFile;

  12. /**
  13.  * 说明:上传文件
  14.  * 作者:FH Admin
  15.  * 官网:fhadmin.cn
  16.  */
  17. public class FileUpload {

  18.     /**上传文件
  19.      * @param file             //文件对象
  20.      * @param filePath        //上传路径
  21.      * @param fileName        //文件名
  22.      * @return 文件名
  23.      */
  24.     public static String fileUp(MultipartFile file, String filePath, String fileName){
  25.         String extName = ""; // 扩展名格式:
  26.         try {
  27.             if (file.getOriginalFilename().lastIndexOf(".") >= 0){
  28.                 extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
  29.             }
  30.             copyFile(file.getInputStream(), filePath, fileName+extName).replaceAll("-", "");
  31.         } catch (IOException e) {
  32.             System.out.println(e);
  33.         }
  34.         return fileName+extName;
  35.     }
  36.     
  37.     /**
  38.      * 写文件到当前目录的upload目录中
  39.      * @param in
  40.      * @param fileName
  41.      * @throws IOException
  42.      */
  43.     public static String copyFile(InputStream in, String dir, String realName)
  44.             throws IOException {
  45.         File file = mkdirsmy(dir,realName);
  46.         FileUtils.copyInputStreamToFile(in, file);
  47.         in.close();
  48.         return realName;
  49.     }
  50.     
  51.     
  52.     /**判断路径是否存在,否:创建此路径
  53.      * @param dir 文件路径
  54.      * @param realName 文件名
  55.      * @throws IOException
  56.      */
  57.     public static File mkdirsmy(String dir, String realName) throws IOException{
  58.         File file = new File(dir, realName);
  59.         if (!file.exists()) {
  60.             if (!file.getParentFile().exists()) {
  61.                 file.getParentFile().mkdirs();
  62.             }
  63.             file.createNewFile();
  64.         }
  65.         return file;
  66.     }
  67.     
  68.     
  69.     /**下载网络图片上传到服务器上
  70.      * @param httpUrl 图片网络地址
  71.      * @param filePath    图片保存路径
  72.      * @param myFileName 图片文件名(null时用网络图片原名)
  73.      * @return    返回图片名称
  74.      */
  75.     public static String getHtmlPicture(String httpUrl, String filePath , String myFileName) {
  76.         
  77.         URL url;                        //定义URL对象url
  78.         BufferedInputStream in;            //定义输入字节缓冲流对象in
  79.         FileOutputStream file;            //定义文件输出流对象file
  80.         try {
  81.             String fileName = null == myFileName?httpUrl.substring(httpUrl.lastIndexOf("/")).replace("/", ""):myFileName; //图片文件名(null时用网络图片原名)
  82.             url = new URL(httpUrl);        //初始化url对象
  83.             in = new BufferedInputStream(url.openStream());                                    //初始化in对象,也就是获得url字节流
  84.             //file = new FileOutputStream(new File(filePath +"\\"+ fileName));
  85.             file = new FileOutputStream(mkdirsmy(filePath,fileName));
  86.             int t;
  87.             while ((t = in.read()) != -1) {
  88.                 file.write(t);
  89.             }
  90.             file.close();
  91.             in.close();
  92.             return fileName;
  93.         } catch (MalformedURLException e) {
  94.             e.printStackTrace();
  95.         } catch (FileNotFoundException e) {
  96.             e.printStackTrace();
  97.         } catch (IOException e) {
  98.             e.printStackTrace();
  99.         }
  100.         return null;
  101.         
  102.     }
  103. }



  104. package com.fh.util;

  105. import java.io.BufferedOutputStream;
  106. import java.io.OutputStream;
  107. import java.net.URLEncoder;

  108. import javax.servlet.http.HttpServletResponse;

  109. /**
  110.  * 说明:下载文件
  111.  * 作者:FH Admin
  112.  * 官网:fhadmin.cn
  113.  */
  114. public class FileDownload {

  115.     /**
  116.      * @param response
  117.      * @param filePath        //文件完整路径(包括文件名和扩展名)
  118.      * @param fileName        //下载后看到的文件名
  119.      * @return 文件名
  120.      */
  121.     public static void fileDownload(final HttpServletResponse response, String filePath, String fileName) throws Exception{
  122.         byte[] data = FileUtil.toByteArray2(filePath);
  123.      fileName = URLEncoder.encode(fileName, "UTF-8");
  124.      response.reset();
  125.      response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
  126.      response.addHeader("Content-Length", "" + data.length);
  127.      response.setContentType("application/octet-stream;charset=UTF-8");
  128.      OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
  129.      outputStream.write(data);
  130.      outputStream.flush();
  131.      outputStream.close();
  132.      response.flushBuffer();
  133.     }

  134. }



阅读(1090) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~