优化缩略图查看接口
This commit is contained in:
@@ -179,13 +179,6 @@ public class SystemConfigController {
|
|||||||
String fileUrl = "";
|
String fileUrl = "";
|
||||||
if(fileUploadType == 1) {
|
if(fileUploadType == 1) {
|
||||||
fileUrl = systemConfigService.getFileUrlLocal(imgPath);
|
fileUrl = systemConfigService.getFileUrlLocal(imgPath);
|
||||||
File file = new File(fileUrl);
|
|
||||||
if(!file.exists()){
|
|
||||||
response.setStatus(404);
|
|
||||||
throw new RuntimeException("文件不存在..");
|
|
||||||
}
|
|
||||||
response.setContentType("application/force-download");// 设置强制下载不打开
|
|
||||||
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes("UTF-8"),"iso-8859-1"));
|
|
||||||
inputStream = new BufferedInputStream(new FileInputStream(fileUrl));
|
inputStream = new BufferedInputStream(new FileInputStream(fileUrl));
|
||||||
} else if(fileUploadType == 2) {
|
} else if(fileUploadType == 2) {
|
||||||
fileUrl = systemConfigService.getFileUrlAliOss(imgPath);
|
fileUrl = systemConfigService.getFileUrlAliOss(imgPath);
|
||||||
@@ -240,6 +233,7 @@ public class SystemConfigController {
|
|||||||
if(StringUtil.isEmpty(imgPath) || imgPath=="null"){
|
if(StringUtil.isEmpty(imgPath) || imgPath=="null"){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
InputStream inputStream = null;
|
||||||
OutputStream outputStream = null;
|
OutputStream outputStream = null;
|
||||||
try {
|
try {
|
||||||
imgPath = imgPath.replace("..", "");
|
imgPath = imgPath.replace("..", "");
|
||||||
@@ -247,17 +241,20 @@ public class SystemConfigController {
|
|||||||
imgPath = imgPath.substring(0, imgPath.length() - 1);
|
imgPath = imgPath.substring(0, imgPath.length() - 1);
|
||||||
}
|
}
|
||||||
String fileUrl = "";
|
String fileUrl = "";
|
||||||
File file = null;
|
|
||||||
if(fileUploadType == 1) {
|
if(fileUploadType == 1) {
|
||||||
fileUrl = systemConfigService.getFileUrlLocal(imgPath);
|
fileUrl = systemConfigService.getFileUrlLocal(imgPath);
|
||||||
file = new File(fileUrl);
|
inputStream = new BufferedInputStream(new FileInputStream(fileUrl));
|
||||||
} else if(fileUploadType == 2) {
|
} else if(fileUploadType == 2) {
|
||||||
fileUrl = systemConfigService.getFileUrlAliOss(imgPath);
|
fileUrl = systemConfigService.getFileUrlAliOss(imgPath);
|
||||||
file = FileUtils.UrltoFile(fileUrl);
|
URL url = new URL(fileUrl);
|
||||||
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
|
conn.setRequestMethod("GET");
|
||||||
|
conn.setConnectTimeout(5 * 1000);
|
||||||
|
inputStream = conn.getInputStream();// 通过输入流获取图片数据
|
||||||
}
|
}
|
||||||
int index = fileUrl.lastIndexOf(".");
|
int index = fileUrl.lastIndexOf(".");
|
||||||
String ext = fileUrl.substring(index + 1);
|
String ext = fileUrl.substring(index + 1);
|
||||||
BufferedImage image = systemConfigService.getImageMini(file, 40);
|
BufferedImage image = systemConfigService.getImageMini(inputStream, 40);
|
||||||
outputStream = response.getOutputStream();
|
outputStream = response.getOutputStream();
|
||||||
ImageIO.write(image, ext, outputStream);
|
ImageIO.write(image, ext, outputStream);
|
||||||
response.flushBuffer();
|
response.flushBuffer();
|
||||||
|
|||||||
@@ -293,8 +293,8 @@ public class SystemConfigService {
|
|||||||
return linkUrl + filePath + "/" + imgPath;
|
return linkUrl + filePath + "/" + imgPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
public BufferedImage getImageMini(File file, int w) throws Exception {
|
public BufferedImage getImageMini(InputStream inputStream, int w) throws Exception {
|
||||||
BufferedImage img = ImageIO.read(file);
|
BufferedImage img = ImageIO.read(inputStream);
|
||||||
//获取图片的长和宽
|
//获取图片的长和宽
|
||||||
int width = img.getWidth();
|
int width = img.getWidth();
|
||||||
int height = img.getHeight();
|
int height = img.getHeight();
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
package com.jsh.erp.utils;
|
package com.jsh.erp.utils;
|
||||||
|
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -353,26 +350,4 @@ public class FileUtils {
|
|||||||
fileName = fileName.replace("=","").replace(",","").replace("&","");
|
fileName = fileName.replace("=","").replace(",","").replace("&","");
|
||||||
return fileName;
|
return fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
//将Url转换为File
|
|
||||||
public static File UrltoFile(String url) throws Exception {
|
|
||||||
HttpURLConnection httpUrl = (HttpURLConnection) new URL(url).openConnection();
|
|
||||||
httpUrl.connect();
|
|
||||||
InputStream ins=httpUrl.getInputStream();
|
|
||||||
File file = new File(System.getProperty("java.io.tmpdir") + File.separator + "xie");//System.getProperty("java.io.tmpdir")缓存
|
|
||||||
if (file.exists()) {
|
|
||||||
file.delete();//如果缓存中存在该文件就删除
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user