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