forked from GitHubyen/Util
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUpload.java
More file actions
58 lines (52 loc) · 1.53 KB
/
FileUpload.java
File metadata and controls
58 lines (52 loc) · 1.53 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
* DateTime: 2016/9/18 21:41
* 功能:文件上传
* 思路:
*/
public class FileUpload {
//测试
public static void main(String[] args) {
}
/**
* 写文件到当前目录的upload目录中
* @param in
* @param dir
* @param realName
* @return
*/
public static String copyFile(InputStream in,String dir,String realName) throws IOException {
File file=new File(dir,realName);
if(!file.exists()){
if(!file.getParentFile().exists()){
file.getParentFile().mkdir();
}
file.createNewFile();
}
FileUtils.copyInputStreamToFile(in,file);
return realName;
}
/**
* 文件上传
* @param file 文件对象
* @param filePath 上传路径
* @param fileName 文件名
* @return
*/
public static String fileUp(MultipartFile file,String filePath,String fileName){
String extName=""; //后缀
try {
if(file.getOriginalFilename().lastIndexOf(".")>=0){
extName=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
}
copyFile(file.getInputStream(),filePath,fileName+extName).replace("-","");
} catch ( IOException e ) {
e.printStackTrace();
}
return fileName+extName;
}
}