0
A
回答
0
你可以使用Spring的RestTemplate下載文件
RestTemplate templ = new RestTemplate();
byte[] downloadedBytes = templ.getForObject(url, byte[].class);
用標準java或第三方庫提取內容。
實施例實用,適於從這裏:http://www.codejava.net/java-se/file-io/programmatically-extract-a-zip-file-using-java
package com.test;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class ZipHelper {
private static final int BUFFER_SIZE = 4096;
public static void unzip(byte[] data, String dirName) throws IOException {
File destDir = new File(dirName);
if (!destDir.exists()) {
destDir.mkdir();
}
ZipInputStream zipIn = new ZipInputStream(new ByteArrayInputStream(data));
ZipEntry entry = zipIn.getNextEntry();
while (entry != null) {
String filePath = dirName + File.separator + entry.getName();
if (!entry.isDirectory()) {
// if the entry is a file, extracts it
extractFile(zipIn, filePath);
} else {
// if the entry is a directory, make the directory
File dir = new File(filePath);
dir.mkdir();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
}
private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
byte[] bytesIn = new byte[BUFFER_SIZE];
int read = 0;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
bos.close();
}
}
然後使用此工具像這樣:
ZipHelper.unzip(downloadedBytes, "/path/to/directory");
0
的requesdt體應該是這樣的:
@RequestMapping(value = "/push/{id}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public String pushTransactions(@PathVariable("id") String id, @RequestBody byte[] str) throws MessagingException, JsonParseException, JsonMappingException, IOException {
,並在控制器,你可以這樣做:
GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(str));
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len;
while ((len = gis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
gis.close();
out.close();
相關問題
- 1. HTTP GET請求獲取XML文件的二進制內容
- 2. 使用HTTP GET請求從Web Api返回文件內容
- 3. Ruby:下載zip文件並提取
- 4. 通過HTTP GET請求從Google Drive下載文件
- 5. 使用C#HTTP GET下載文件
- 6. JSP:如何讀取HTTP POST/GET請求的內容
- 7. 使用HTTP GET請求angularjs
- 8. 如何從POST請求的HTTP正文提取二進制文件內容?
- 9. HTTP GET請求()
- 10. 使用7-Zip和PowerShell提取歸檔文件的內容
- 11. Gradle,提取依賴zip和使用其文件中的內容?
- 12. 節點HTTP GET請求無法處理希伯來文內容
- 13. 如何使用VB從Zip文件中提取所有內容?
- 14. http POST vs GET請求獲取發送XML文件的文檔
- 15. Java HTTP請求:獲取內容大小
- 16. 使用$ .post或$ .get返回一個可下載的zip文件
- 17. 如何在不下載內容的情況下執行GET請求?
- 18. HTTP請求失敗!與文件獲取內容
- 19. 使用XML HTTP請求下載大文件
- 20. 使用一個http請求下載文件並且不壓縮
- 21. Android - HTTP GET請求
- 22. 假HTTP GET請求
- 23. Android - HTTP GET請求
- 24. HTTP請求和GET
- 25. 使用MVVM的HTTP GET請求
- 26. Netty HTTP客戶端下載zip文件
- 27. PHP還是Apache? zip文件的內容是diplayed而不是下載zip文件
- 28. HTTP請求超時和內容下載時間
- 29. 使用Ajax請求下載文件Extjs4
- 30. 下載.zip文件
謝謝,我需要解碼響應? r認爲圖書館使用了? – KPN24
對不起,你是什麼意思「r認爲」? –
對不起,'or',類似那樣?:Inflater decompressor = new Inflater(); \t \t \t decompressor.setInput(file); \t \t \t ByteArrayOutputStream bos = new ByteArrayOutputStream(file.length); \t \t \t byte [] buf = new byte [1024]; \t \t \t而{ \t \t \t詮釋計數= decompressor.inflate(BUF)(decompressor.finished()!); \t \t \t bos.write(buf,0,count); \t \t \t} \t \t \t bos.close(); \t \t byte [] decompressedData = bos.toByteArray(); – KPN24