You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
3.1 KiB
Java
97 lines
3.1 KiB
Java
1 year ago
|
package com.weaver.seconddev.sxjg.util;
|
||
|
|
||
|
import java.io.*;
|
||
|
import java.net.HttpURLConnection;
|
||
|
import java.net.URL;
|
||
|
import java.nio.file.Files;
|
||
|
import java.nio.file.Path;
|
||
|
import java.nio.file.Paths;
|
||
|
import java.util.Base64;
|
||
|
|
||
|
import cn.com.infosec.netsign.agent.util.IOUtils;
|
||
|
import com.google.gson.Gson;
|
||
|
public class Test {
|
||
|
|
||
|
public static void main(String[] args) throws Exception {
|
||
|
System.out.println(1);
|
||
|
InputStream inputStream = getInputStreamByUrl("http://10.73.4.5:20600/papi/file/local/download/897827c9a77c4199b629cc1d7b848b52?tenantKey=tl2qvzz346&response-expires=V2VkIERlYyAyNyAxNDo0NzozOSBDU1QgMjAyMw==&response-content-disposition=YXR0YWNobWVudDtmaWxlbmFtZT0i5paw5bu6IOaWh-acrOaWh-ahoyAoNCkudHh0Ig==&download=MA==");
|
||
|
System.out.println("2222222222222222222222222222");
|
||
|
byte[] fileBytesArray = null;
|
||
|
fileBytesArray = readBytesFromInputStream(inputStream);
|
||
|
System.out.println("3333333333333");
|
||
|
String jsonResult = convertToJson("filename", fileBytesArray);
|
||
|
System.out.println(jsonResult);
|
||
|
}
|
||
|
|
||
|
public static InputStream getInputStreamByUrl(String strUrl) {
|
||
|
System.out.println("11111111");
|
||
|
HttpURLConnection conn = null;
|
||
|
try {
|
||
|
URL url = new URL(strUrl);
|
||
|
conn = (HttpURLConnection) url.openConnection();
|
||
|
conn.setRequestMethod("GET");
|
||
|
conn.setConnectTimeout(20 * 1000);
|
||
|
final ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||
|
IOUtils.copy(conn.getInputStream(), output);
|
||
|
return new ByteArrayInputStream(output.toByteArray());
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
} finally {
|
||
|
try {
|
||
|
if (conn != null) {
|
||
|
conn.disconnect();
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
}
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
|
||
|
public static byte[] readBytesFromInputStream(InputStream input) {
|
||
|
System.out.println("111111111111111");
|
||
|
try {
|
||
|
System.out.println("2222222222222222");
|
||
|
return org.apache.commons.io.IOUtils.toByteArray(input);
|
||
|
} catch (IOException e) {
|
||
|
System.out.println("2222222");
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
public static String convertToJson(String fileName, byte[] fileBytesArray) {
|
||
|
Gson gson = new Gson();
|
||
|
String jsonResult = gson.toJson(new FileData(fileName, fileBytesArray));
|
||
|
return jsonResult;
|
||
|
}
|
||
|
|
||
|
static class FileData {
|
||
|
private String file_name;
|
||
|
private byte[] filebytes_array;
|
||
|
|
||
|
public FileData(String file_name, byte[] filebytes_array) {
|
||
|
this.file_name = file_name;
|
||
|
this.filebytes_array = filebytes_array;
|
||
|
}
|
||
|
|
||
|
public String getFile_name() {
|
||
|
return file_name;
|
||
|
}
|
||
|
|
||
|
public void setFile_name(String file_name) {
|
||
|
this.file_name = file_name;
|
||
|
}
|
||
|
|
||
|
public byte[] getFilebytes_array() {
|
||
|
return filebytes_array;
|
||
|
}
|
||
|
|
||
|
public void setFilebytes_array(byte[] filebytes_array) {
|
||
|
this.filebytes_array = filebytes_array;
|
||
|
}
|
||
|
}
|
||
|
}
|