网络URL获取流
/**
* 网络URL 转 InputStream
*
* @param url
* @return
*/
private static InputStream getImageStream(String url) {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
return inputStream;
}
} catch (IOException e) {
log.info("获取网络图片出现异常,图片路径为:" + url);
e.printStackTrace();
}
return null;
}
网络URL 转 MultipartFile
/**
* 网络URL 转 MultipartFile
*
* @param newFileName 新文件名称
* @param oldFileName 旧文件名称
* @param conCentType 原文件ContentType mage/jpeg
* @param url 网络图片地址
* @return
*/
public static MultipartFile getMultipartFile(String newFileName, String oldFileName, String conCentType, String url) throws IOException {
InputStream imageStream = getImageStream(url);
return new MockMultipartFile(newFileName, oldFileName, conCentType, imageStream);
}
文件传给第三方接口
private static int uploadFile(File file, String s3Path) throws IOException {
URL url = new URL(U);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Set up the connection properties
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + "*****");
OutputStream outputStream = null;
PrintWriter writer = null;
FileInputStream fileInputStream = null;
BufferedReader reader = null;
try {
outputStream = connection.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true);
fileInputStream = new FileInputStream(file);
// Add the file part to the request
String boundary = "*****";
writer.append("--").append(boundary).append("\r\n");
writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"").append(s3Path).append("\"\r\n");
writer.append("Content-Type: ").append("application/octet-stream").append("\r\n\r\n");
writer.flush();
// Write the file content
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
writer.append("\r\n");
writer.append("--").append(boundary).append("--").append("\r\n");
writer.flush();
// Get the response from the server
int responseCode = connection.getResponseCode();
System.out.println("Response Code: " + responseCode);
if (responseCode != 200) {
return 500;
}
// Read the response from the server (if needed)
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}} finally {
connection.disconnect();
//关闭流
if (outputStream != null) {
outputStream.close();
}
if (writer != null) {
writer.close();
}
if (fileInputStream != null) {
fileInputStream.close();
}
if (reader != null) {
reader.close();
}
}
return 200;
}
MultipartFile转File
public static File toFile(MultipartFile multipartFile) {
// 获取文件名
String fileName = multipartFile.getOriginalFilename();
// 获取文件后缀
String prefix = "." + getExtensionName(fileName);
File file = null;
try {
// 用uuid作为文件名,防止生成的临时文件重复
file = File.createTempFile(IdUtil.simpleUUID(), prefix);
// MultipartFile to File
multipartFile.transferTo(file);
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
inputStream 转 Filestatic File inputStreamToFile(InputStream ins, String name) throws Exception {
File file = new File(System.getProperty("java.io.tmpdir") + File.separator + name);
if (file.exists()) {
return file;
}
OutputStream os = new FileOutputStream(file);
int bytesRead;
int len = 8192;
byte[] buffer = new byte[len];
while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
return file;
}