webservice端生成一個pdf,我這邊生成后的檔案地址是我本地盤里這個檔案的地址,客戶端只獲得這個地址下載不到pdf;
我這個地址要怎么寫客戶端才能下載到?
uj5u.com熱心網友回復:
寫一個方法 private void downloadFile(HttpServletResponse response, File file) {
/* 設定檔案ContentType型別,這樣設定,會自動判斷下載檔案型別 */
response.setContentType("multipart/form-data");
/* 設定檔案頭:最后一個引數是設定下載檔案名 */
response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
try (
InputStream ins = new FileInputStream(file);
OutputStream os = response.getOutputStream()
) {
byte[] b = new byte[1024];
int len;
while ((len = ins.read(b)) > 0) {
os.write(b, 0, len);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}這樣就可以了
uj5u.com熱心網友回復:
不是,我這邊客戶端只能接受一個pdf的url來下載,別人寫好的呼叫。有沒有辦法能夠將我的檔案生成到他的專案中去,或者我這邊pdf的路徑能夠讓他直接下載?uj5u.com熱心網友回復:
沒有辦法能夠將你的檔案生成到他的專案中去, 要么要他寫下載方法 你有地址那就File file = new File(path) 這不就完了? 我沒怎么明白 你這里拿到你個url的意義是啥?uj5u.com熱心網友回復:
webservice介面最好用什么形式讓客戶端下載檔案呢?轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/135796.html
標籤:Web 開發
