访问远程服务器下载图片并保存在本地的服务器。
package com.nbomb.route.util;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.ServerConfig;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.ArrayList;
/**
* @author SunWenKe
* @version v1.0
* @date 2023-06-25 11:56
*/
@Component
public class ImageDownLoad {
@Autowired
private ServerConfig serverConfig;
public AjaxResult extracted(String urls) throws IOException {
DownloadAndConvertToMultipartFileExample downloadAndConvertToMultipartFileExample = new DownloadAndConvertToMultipartFileExample();
MultipartFile multipartFile = downloadAndConvertToMultipartFileExample.downloadAndConvertToMultipartFile(urls);
String filePath = RuoYiConfig.getUploadPath();
// 上传并返回新文件名称
AjaxResult ajax = AjaxResult.success();
String string = multipartFile.toString();
// System.out.println("图片------------>"+string);
String fileName = FileUploadUtils.upload(filePath, multipartFile);
ajax.put("fileName", fileName);
return ajax;
}
}
下载方法
package com.nbomb.route.util;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
public class DownloadAndConvertToMultipartFileExample {
private final OkHttpClient okHttpClient = new OkHttpClient();
public MultipartFile downloadAndConvertToMultipartFile(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
Response response = okHttpClient.newCall(request).execute();
InputStream inputStream = response.body().byteStream();
String originalFilename = getOriginalFileName(response);
return new MockMultipartFile(
UUID.randomUUID().toString(), // 文件名
originalFilename, // 显示文件名
getContentType(response), // 内容类型
inputStream // 输入流
);
}
private String getOriginalFileName(Response response) {
// 从响应头中获取文件名
String contentDispositionHeader = response.header("Content-Disposition");
if (contentDispositionHeader != null) {
int indexOfFilename = contentDispositionHeader.indexOf("filename=");
if (indexOfFilename != -1) {
return contentDispositionHeader.substring(indexOfFilename + 9)
.replaceAll("\"", ""); // 去掉引号
}
}
// 如果响应头中没有文件名,则从 URL 中获取
int lastSlashIndex = response.request().url().toString().lastIndexOf("/");
if (lastSlashIndex != -1) {
return response.request().url().toString().substring(lastSlashIndex + 1);
}
// 如果 URL 中也没有文件名,则使用默认值
String value = String.valueOf(System.currentTimeMillis());
return value;
}
private String getContentType(Response response) {
String contentTypeHeader = response.header("Content-Type");
return contentTypeHeader != null ? contentTypeHeader : "application/octet-stream";
}
}