fix: 文件上传转换工具
This commit is contained in:
@@ -122,11 +122,6 @@
|
||||
<groupId>javax.persistence</groupId>
|
||||
<artifactId>javax.persistence-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
@@ -19,8 +19,8 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.wfc.common.core.domain.R;
|
||||
import org.wfc.common.core.exception.ServiceException;
|
||||
import org.wfc.common.core.utils.file.MultipartFileUtil;
|
||||
import org.wfc.payment.pay.wxpay.service.IWxPayService;
|
||||
import org.wfc.payment.utils.MultipartFileUtil;
|
||||
import org.wfc.system.api.RemoteFileService;
|
||||
import org.wfc.system.api.domain.SysFile;
|
||||
import org.wfc.user.api.RemoteUUserService;
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
package org.wfc.payment.utils;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* MultipartFile和File互转工具类
|
||||
*/
|
||||
public class MultipartFileUtil {
|
||||
|
||||
/**
|
||||
* 输入流转MultipartFile
|
||||
*
|
||||
* @param fileName
|
||||
* @param inputStream
|
||||
* @return
|
||||
*/
|
||||
public static MultipartFile getMultipartFile(String fileName, InputStream inputStream) {
|
||||
MultipartFile multipartFile = null;
|
||||
try {
|
||||
multipartFile = new MockMultipartFile(fileName, fileName,
|
||||
ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return multipartFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取网络文件
|
||||
*
|
||||
* @param url 文件地址
|
||||
* @param fileName 文件名称(需带文件名后缀)
|
||||
* @return
|
||||
*/
|
||||
public static MultipartFile getMultipartFile(String url, String fileName) {
|
||||
HttpResponse response = HttpRequest.get(url).execute();
|
||||
InputStream inputStream = response.bodyStream();
|
||||
return MultipartFileUtil.getMultipartFile(fileName, inputStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* File 转MultipartFile
|
||||
*
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
public static MultipartFile getMultipartFile(File file) {
|
||||
FileInputStream fileInputStream = null;
|
||||
MultipartFile multipartFile = null;
|
||||
try {
|
||||
fileInputStream = new FileInputStream(file);
|
||||
multipartFile = new MockMultipartFile(file.getName(), file.getName(),
|
||||
ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return multipartFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* MultipartFileUtil 转File
|
||||
*
|
||||
* @param multipartFile
|
||||
* @return
|
||||
*/
|
||||
public static File getFile(MultipartFile multipartFile) {
|
||||
String fileName = multipartFile.getOriginalFilename();
|
||||
File file = new File(fileName);
|
||||
OutputStream out = null;
|
||||
try {
|
||||
out = new FileOutputStream(file);
|
||||
byte[] ss = multipartFile.getBytes();
|
||||
for (byte s : ss) {
|
||||
out.write(s);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (out != null) {
|
||||
try {
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
// File f = new File(file.toURI());
|
||||
// if (f.delete()) {
|
||||
// System.out.println("删除成功");
|
||||
// } else {
|
||||
// System.out.println("删除失败");
|
||||
// }
|
||||
return file;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user