解决网络图片不展示的问题
This commit is contained in:
@@ -6,6 +6,7 @@ import com.jsh.erp.service.systemConfig.SystemConfigService;
|
|||||||
import com.jsh.erp.service.user.UserService;
|
import com.jsh.erp.service.user.UserService;
|
||||||
import com.jsh.erp.service.userBusiness.UserBusinessService;
|
import com.jsh.erp.service.userBusiness.UserBusinessService;
|
||||||
import com.jsh.erp.utils.BaseResponseInfo;
|
import com.jsh.erp.utils.BaseResponseInfo;
|
||||||
|
import com.jsh.erp.utils.FileUtils;
|
||||||
import com.jsh.erp.utils.StringUtil;
|
import com.jsh.erp.utils.StringUtil;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
@@ -246,14 +247,17 @@ 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);
|
||||||
} else if(fileUploadType == 2) {
|
} else if(fileUploadType == 2) {
|
||||||
fileUrl = systemConfigService.getFileUrlAliOss(imgPath);
|
fileUrl = systemConfigService.getFileUrlAliOss(imgPath);
|
||||||
|
file = FileUtils.UrltoFile(fileUrl);
|
||||||
}
|
}
|
||||||
int index = fileUrl.lastIndexOf(".");
|
int index = fileUrl.lastIndexOf(".");
|
||||||
String ext = fileUrl.substring(index + 1);
|
String ext = fileUrl.substring(index + 1);
|
||||||
BufferedImage image = systemConfigService.getImageMini(fileUrl, 40);
|
BufferedImage image = systemConfigService.getImageMini(file, 40);
|
||||||
outputStream = response.getOutputStream();
|
outputStream = response.getOutputStream();
|
||||||
ImageIO.write(image, ext, outputStream);
|
ImageIO.write(image, ext, outputStream);
|
||||||
response.flushBuffer();
|
response.flushBuffer();
|
||||||
|
|||||||
@@ -35,7 +35,10 @@ import javax.imageio.ImageIO;
|
|||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.*;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -290,8 +293,8 @@ public class SystemConfigService {
|
|||||||
return linkUrl + filePath + "/" + imgPath;
|
return linkUrl + filePath + "/" + imgPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
public BufferedImage getImageMini(String fileUrl, int w) throws Exception {
|
public BufferedImage getImageMini(File file, int w) throws Exception {
|
||||||
BufferedImage img = ImageIO.read(new File(fileUrl));
|
BufferedImage img = ImageIO.read(file);
|
||||||
//获取图片的长和宽
|
//获取图片的长和宽
|
||||||
int width = img.getWidth();
|
int width = img.getWidth();
|
||||||
int height = img.getHeight();
|
int height = img.getHeight();
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package com.jsh.erp.utils;
|
|||||||
|
|
||||||
import org.springframework.util.StringUtils;
|
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.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -352,4 +354,25 @@ public class FileUtils {
|
|||||||
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