guoyong 4 лет назад
Родитель
Сommit
b141d25ceb
1 измененных файлов с 20 добавлено и 1 удалено
  1. 20 1
      src/main/java/com/aoyang/tms/service/impl/JxlsExportTestServiceImpl.java

+ 20 - 1
src/main/java/com/aoyang/tms/service/impl/JxlsExportTestServiceImpl.java

@@ -18,6 +18,8 @@ import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBo
 import javax.annotation.Resource;
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpServletResponse;
 import java.io.*;
 import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashMap;
 import java.util.List;
 import java.util.List;
@@ -233,10 +235,27 @@ public class JxlsExportTestServiceImpl implements JxlsExportTestService {
 
 
     }
     }
 
 
+    /**
+     * 从http中获得 Inputstream
+     *
+     * @param url http地址
+     * @return inputstream
+     * @throws java.io.IOException IOException
+     */
+    private static InputStream getInputStreamFromHttp(String url) throws IOException {
+        URL downUrl = new URL(url);
+        HttpURLConnection connection = (HttpURLConnection) downUrl.openConnection();
+        connection.connect();
+        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
+            return connection.getInputStream();
+        }
+        return null;
+    }
+
     //获取图片的字节码,临时使用
     //获取图片的字节码,临时使用
     private static byte[] getImageBytes(String imagePath) throws IOException {
     private static byte[] getImageBytes(String imagePath) throws IOException {
         byte imageBytes[] = null;
         byte imageBytes[] = null;
-        InputStream is = JxlsExportTestServiceImpl.class.getClass().getResourceAsStream(imagePath);
+        InputStream is = getInputStreamFromHttp(imagePath);
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         byte bytes[] = new byte[1024];
         byte bytes[] = new byte[1024];
         int lens = -1;
         int lens = -1;