upload
远程上传
public static String remoteUpload(String url, String uploadFilePath, String types) throws IOException {
URL objectUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)objectUrl.openConnection();
conn.connect();
InputStream inputStream = conn.getInputStream();
File newFile = new File(uploadFilePath + SnowflakeIdGenerator.nextId() + types);
byte[] bytes = new byte[1024];
FileOutputStream downloadFile = new FileOutputStream(newFile);
int index;
while((index = inputStream.read(bytes)) != -1) {
downloadFile.write(bytes, 0, index);
downloadFile.flush();
}
inputStream.close();
downloadFile.close();
return newFile.getPath().replaceAll("\\\\", "/").replace(uploadFilePath, "/upload");
}
上传
public static String upload(File file, String uploadFilePath) {
LocalDate var10000 = LocalDate.now();
String date = "/" + var10000.format(DateTimeFormatter.ofPattern("yyyy/MM/dd")) + "/";
File path = new File(uploadFilePath + date);
if (!path.exists()) {
path.mkdirs();
}
File newFile = new File(uploadFilePath + date + SnowflakeIdGenerator.nextId() + StringUtil.getFileType(file));
try {
if (!newFile.exists()) {
newFile.createNewFile();
}
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(newFile);
byte[] data = new byte[8192];
boolean var8 = false;
int len;
while((len = fis.read(data)) != -1) {
fos.write(data, 0, len);
}
fis.close();
fos.close();
} catch (IOException var12) {
var12.printStackTrace();
} finally {
MultipartFileToFile.delteTempFile(file);
}
return newFile.getPath().replaceAll("\\\\", "/").replace(uploadFilePath, "/upload");
}
删除
public static void deleteFile(String uploadFilePath, String[] path) {
if (path != null && path.length > 0) {
File file = null;
String[] var3 = path;
int var4 = path.length;
for(int var5 = 0; var5 < var4; ++var5) {
String p = var3[var5];
file = new File(uploadFilePath + p.replace("/upload", ""));
if (file.exists()) {
file.delete();
}
}
}
}
Down
@GetMapping("down")
public void down(String fileName, String path) throws IOException {
if (fileName == null) {
throw new ExRuntimeException("文件不存在");
} else {
String var10002 = this.config.getUploadPath();
File file = new File(var10002 + path.replace("/upload", ""));
if (file.exists()) {
this.response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
this.response.setContentType("application/octet-stream");
this.response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = this.response.getOutputStream();
for (int i = bis.read(buffer); i != -1; i = bis.read(buffer)) {
os.write(buffer, 0, i);
}
} catch (Exception var12) {
throw new ExRuntimeException(var12.getMessage());
} finally {
if (bis != null) {
bis.close();
}
if (fis != null) {
fis.close();
}
}
}
}
}
下载工具类
@bean
public class YmlConfig {
@value("edp.config.uploadPath")
private String uploadPath;
@value("edp.config.uploadWinPath")
private String uploadWinPath;
public YmlConfig() {
}
public String getUploadPath() {
String props = System.getProperties().getProperty("os.name");
if (props.toLowerCase().contains(ZdConst.windows)) {
return this.uploadWinPath != null ? this.uploadWinPath : " C:/data/upload/";
} else {
return this.uploadPath != null ? this.uploadPath : "/root/upload/";
}
}
}