Commit fc73deb3 authored by 陈精华's avatar 陈精华

修复上传到demo中的压缩文件及pdf预览异常

parent 40ac4b1e
...@@ -56,9 +56,10 @@ public class DownloadUtils { ...@@ -56,9 +56,10 @@ public class DownloadUtils {
} }
try { try {
URL url = new URL(urlStr); URL url = new URL(urlStr);
OutputStream os = new FileOutputStream(new File(realPath));
if (url.getProtocol() != null && url.getProtocol().toLowerCase().startsWith("http")) { if (url.getProtocol() != null && url.getProtocol().toLowerCase().startsWith("http")) {
saveToOutputStreamFromUrl(urlStr, os); byte[] bytes = getBytesFromUrl(urlStr);
OutputStream os = new FileOutputStream(new File(realPath));
saveBytesToOutStream(bytes, os);
} else if (url.getProtocol() != null && "ftp".equals(url.getProtocol().toLowerCase())) { } else if (url.getProtocol() != null && "ftp".equals(url.getProtocol().toLowerCase())) {
String ftpUsername = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME); String ftpUsername = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME);
String ftpPassword = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD); String ftpPassword = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD);
...@@ -88,21 +89,24 @@ public class DownloadUtils { ...@@ -88,21 +89,24 @@ public class DownloadUtils {
} }
} }
public boolean saveToOutputStreamFromUrl(String urlStr, OutputStream os) throws IOException { public byte[] getBytesFromUrl(String urlStr) throws IOException {
InputStream is = getInputStreamFromUrl(urlStr); InputStream is = getInputStreamFromUrl(urlStr);
if (is != null) { if (is != null) {
copyStream(is, os); return getBytesFromStream(is);
} else { } else {
urlStr = URLUtil.normalize(urlStr, true, true); urlStr = URLUtil.normalize(urlStr, true, true);
is = getInputStreamFromUrl(urlStr); is = getInputStreamFromUrl(urlStr);
if (is != null) { if (is == null) {
copyStream(is, os); logger.error("文件下载异常:url:{}", urlStr);
} else { throw new IOException("文件下载异常:url:" + urlStr);
os.close();
return false;
} }
return getBytesFromStream(is);
} }
return true; }
public void saveBytesToOutStream(byte[] b, OutputStream os) throws IOException {
os.write(b);
os.close();
} }
private InputStream getInputStreamFromUrl(String urlStr) { private InputStream getInputStreamFromUrl(String urlStr) {
...@@ -119,14 +123,17 @@ public class DownloadUtils { ...@@ -119,14 +123,17 @@ public class DownloadUtils {
} }
} }
private void copyStream(InputStream is, OutputStream os) throws IOException { private byte[] getBytesFromStream(InputStream is) throws IOException {
byte[] bs = new byte[1024]; ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len; byte[] buffer = new byte[1024];
while (-1 != (len = is.read(bs))) { int len = 0;
os.write(bs, 0, len); while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
} }
byte[] b = baos.toByteArray();
is.close(); is.close();
os.close(); baos.close();
return b;
} }
/** /**
......
...@@ -88,7 +88,8 @@ public class OnlinePreviewController { ...@@ -88,7 +88,8 @@ public class OnlinePreviewController {
public void getCorsFile(String urlPath, HttpServletResponse response) { public void getCorsFile(String urlPath, HttpServletResponse response) {
logger.info("下载跨域pdf文件url:{}", urlPath); logger.info("下载跨域pdf文件url:{}", urlPath);
try { try {
downloadUtils.saveToOutputStreamFromUrl(urlPath, response.getOutputStream()); byte[] bytes = downloadUtils.getBytesFromUrl(urlPath);
downloadUtils.saveBytesToOutStream(bytes, response.getOutputStream());
} catch (IOException e) { } catch (IOException e) {
logger.error("下载跨域pdf文件异常,url:{}", urlPath, e); logger.error("下载跨域pdf文件异常,url:{}", urlPath, e);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment