java实现文件下载功能 根据图片地址下载图片至本地 并上传至oss
/**
* 单个文件下载
*
* @param fileName 单个文件名
* @throws Exception
*/
@Override
public void downOneFile(String fileName) throws Exception {
HttpServletResponse resp = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
File file = new File(FILE_ROOT_PATH + fileName);
if (file.exists()) {
resp.setContentType("application/x-msdownload");
resp.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO-8859-1"));
InputStream inputStream = new FileInputStream(file);
ServletOutputStream ouputStream = resp.getOutputStream();
byte b[] = new byte[1024];
int n;
while ((n = inputStream.read(b)) != -1) {
ouputStream.write(b, 0, n);
}
ouputStream.close();
inputStream.close();
}
}
/**
* 文件批量打包下载
*
* @param fileNameList 多个文件名,用英文逗号分隔开
* @throws IOException
*/
@Override
public void downTogetherAndZip(List<String> fileNameList) throws IOException {
HttpServletResponse resp = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
resp.setContentType("application/x-msdownload");
//暂时设定压缩下载后的文件名字为test.zip
resp.setHeader("Content-Disposition", "attachment;filename=test.zip");
String str = "";
String rt = "\r\n";
ZipOutputStream zos = new ZipOutputStream(resp.getOutputStream());
for (String filename : fileNameList) {
str += filename + rt;
File file = new File(FILE_ROOT_PATH + filename);
zos.putNextEntry(new ZipEntry(filename));
FileInputStream fis = new FileInputStream(file);
byte b[] = new byte[1024];
int n = 0;
while ((n = fis.read(b)) != -1) {
zos.write(b, 0, n);
}
zos.flush();
fis.close();
}
//设置解压文件后的注释内容
zos.setComment("download success:" + rt + str);
zos.flush();
zos.close();
}
将网络图片下载到本地并上传至阿里云oss
public String uploadImg(Integer customerId, CustomerImageType customerImageType, String imageUrl) {
if (StringUtils.isEmpty(imageUrl)) {
return null;
}
//获取图片类型
String imgFormat = imgFormat(imageUrl);
//拼接图片名称
String name = MxcUtils.tagImageFileName(customerId, customerImageType) + "." + imgFormat;
//存储路径
String path = ApiConstants.ID_CARD_PHOTO_PATH;
if (customerImageType.equals(CustomerImageType.ID_CARD_FRONT)) {
path = ApiConstants.ID_CARD_PHOTO_PATH;
}
if (customerImageType.equals(CustomerImageType.PAN)) {
path = ApiConstants.PAN_PHOTO_PATH;
}
if (customerImageType.equals(CustomerImageType.FACE_IMAGE)) {
path = ApiConstants.FACES_PHOTO_PATH;
}
try {
URL url = new URL(imageUrl);
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
//从HTTP响应消息获取状态码
int code = huc.getResponseCode();
if (code == 200) {
//获取输入流
InputStream ips = huc.getInputStream();
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = ips.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
//下载到本地
String filePath = uploadFileByBytes(bos.toByteArray(), name);
File files = new File(filePath);
InputStream inputStream = new FileInputStream(files);
//上传文件至oss
String respath = fileUploadOss(name, path, inputStream);
//清除本地文件
files.delete();
return respath;
}
return "";
} catch (Exception e) {
return "";
}
}
/**
* 将图片储存到 本地
*
* @param bytes
* @param fileName
* @return
* @throws Exception
*/
private String uploadFileByBytes(byte[] bytes, String fileName) throws Exception {
for (int i = 0; i < bytes.length; i++) {
if (bytes[i] < 0) {
bytes[i] += 256;
}
}
String realPath = System.getProperty("user.dir") + File.separator + "upload" + File.separator;
//文件路径
String url = realPath + fileName;
//判断文件路径是否存在,如果没有则新建文件夹
File files = new File(realPath);
if (!files.exists()) {
files.mkdirs();
}
//把文件写入到指定路径下
try (OutputStream out = new BufferedOutputStream(new FileOutputStream(url, false))) {
out.write(bytes);
}
return url;
}
/**
* 上传阿里云
*
* @param saveName 文件名
* @param savePath 路径
* @param inputStream 流
* @return url
*/
private String fileUploadOss(String saveName, String savePath, InputStream inputStream) {
try {
aliyunOSSClient.uploadObject2OSS(saveName, inputStream, savePath);
return aliyunOSSClient.getUrl(aliOssConfig.getBaseDir() + savePath + saveName);
} catch (Exception e) {
log.error("文件上传oss失败!");
e.printStackTrace();
}
return null;
}
public String imgFormat(String path) {
String imgFormat = "jpg";
if (path.contains("bmp")) {
imgFormat = "bmp";
}
if (path.contains("png")) {
imgFormat = "png";
}
if (path.contains("tif")) {
imgFormat = "tif";
}
if (path.contains("gif")) {
imgFormat = "gif";
}
if (path.contains("pcx")) {
imgFormat = "pcx";
}
if (path.contains("tga")) {
imgFormat = "tga";
}
if (path.contains("exif")) {
imgFormat = "exif";
}
if (path.contains("fpx")) {
imgFormat = "fpx";
}
if (path.contains("svg")) {
imgFormat = "svg";
}
if (path.contains("psd")) {
imgFormat = "psd";
}
if (path.contains("cdr")) {
imgFormat = "cdr";
}
if (path.contains("pcd")) {
imgFormat = "pcd";
}
if (path.contains("dxf")) {
imgFormat = "dxf";
}
if (path.contains("ufo")) {
imgFormat = "ufo";
}
if (path.contains("eps")) {
imgFormat = "eps";
}
if (path.contains("ai")) {
imgFormat = "ai";
}
if (path.contains("raw")) {
imgFormat = "raw";
}
if (path.contains("WMF")) {
imgFormat = "WMF";
}
if (path.contains("webp")) {
imgFormat = "webp";
}
if (path.contains("avif")) {
imgFormat = "avif";
}
if (path.contains("apng")) {
imgFormat = "apng";
}
if (path.contains("apng")) {
imgFormat = "apng";
}
if (path.contains("JPEG")) {
imgFormat = "JPEG";
}
return imgFormat;
}