forked from oldbiwang/SSMBlogv2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUploadUtil.java
More file actions
43 lines (38 loc) · 1.04 KB
/
Copy pathUploadUtil.java
File metadata and controls
43 lines (38 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package utils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.FileUtils;
public class UploadUtil {
public static String uploadImg(String filePath, InputStream in) throws IOException {
//获取tomcat 的根目录(webapps 下的路径),editormd.root 在web.xml 中配置
String tomcatRootPath = System.getProperty("editormd.root") + "..";
String resultPath = tomcatRootPath + filePath;
createFile(resultPath);
File realFile = new File(resultPath);
FileUtils.copyInputStreamToFile(in, realFile);
return resultPath;
}
/*
* 创建文件, 如果文件夹不存在将被创建
* */
public static File createFile(String resultPath) {
File file = new File(resultPath);
if(file.exists())
return null;
if(resultPath.endsWith(File.separator))
return null;
if(!file.getParentFile().exists()) {
if(!file.getParentFile().mkdirs())
return null;
}
try {
if(file.createNewFile())
return file;
return null;
} catch(IOException e) {
e.printStackTrace();
}
return null;
}
}