![](/img/trans.png)
[英]downloading file from server to local machine using javascript and java
[英]Downloading a file created in server using Java
我正在嘗試從給定的URL下載文件。 該URL不是直接文件URL。 在瀏覽器中手動提供此URL時,我們會提示您下載/保存。
例如, http://www.my-domain.com/download/type/salary/format/excel
URL中沒有問題,URL中直接包含文件名。 在上面的URL中,服務器根據格式和類型生成文件。
在Java中,我嘗試使用以下代碼下載文件。 文件已創建,但內容僅是域內容,而不是實際的excel數據。
URL url = new URL("http://www.my-domain.com/download/type/salary/format/excel");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
float totalDataRead = 0;
BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
FileOutputStream fos = new FileOutputStream("c:\\test.xls");
BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);
byte[] data = new byte[1024];
int i = 0;
while ((i = in.read(data, 0, 1024)) >= 0) {
totalDataRead = totalDataRead + i;
bout.write(data, 0, i);
}
bout.close();
in.close();
內容就是服務器為該URL發送的內容。 您不能從客戶端對此做任何事情。 例如,如果包含Javascript,則將不會執行。
當您要解決問題時,必須使用適當的工具來完成任務。 足夠的工具可以在poi.apache.org中找到。
看看Apache POI 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.