This commit is contained in:
xiangwbs
2018-03-08 23:39:39 +08:00
parent a0db807a93
commit b34592cb2a
3 changed files with 32 additions and 4 deletions
@@ -2,10 +2,12 @@ package com.xwbing.demo;
import com.alibaba.fastjson.JSONObject;
import com.xwbing.exception.UtilException;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
@@ -26,7 +28,7 @@ public class FileUploadDemo {
materialInfo=new JSONObject();
String originName = multipartFile.getOriginalFilename();//原始名字
materialInfo.put("name",originName);
String fileType = originName.substring(originName.lastIndexOf(".") + 1);//获取文件后缀
String fileType = originName.substring(originName.lastIndexOf(".") + 1);//获取文件后缀名
materialInfo.put("type",fileType);
byte[] data;
try {
@@ -44,4 +46,26 @@ public class FileUploadDemo {
}
return list;
}
/**
* 采用file.Transto 来保存上传的文件
* @param multipartFile
*/
public void fileUpload(CommonsMultipartFile multipartFile) {
if (!multipartFile.isEmpty()) {
String name = multipartFile.getOriginalFilename();//原始名字
String fileType = name.substring(name.lastIndexOf("."));//获取文件后缀
ClassPathResource file = new ClassPathResource("file");
try {
String path = file.getFile().getAbsolutePath();
File targetFile = new File(path+ File.separator+name+fileType);
if (!targetFile.exists()) {
targetFile.createNewFile();
}
multipartFile.transferTo(targetFile);
} catch (IOException e) {
throw new UtilException("读取数据错误");
}
}
}
}
@@ -1,5 +1,7 @@
package com.xwbing.demo;
import org.springframework.core.io.ClassPathResource;
import java.io.*;
/**
@@ -97,9 +99,11 @@ public class IODemo {
/**
* 读取配置文件的2种方式
* 读取文件
*/
String filePath = IODemo.class.getResource("/conf/jdbc.properties").getPath();
InputStream in = IODemo.class.getResourceAsStream("/conf/jdbc.properties");
String filePath = IODemo.class.getResource("/redis.properties").getPath();
InputStream in = IODemo.class.getResourceAsStream("/redis.properties");
ClassPathResource pic = new ClassPathResource("file");
String absolutePath = pic.getFile().getAbsolutePath();
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 326 B