我想从我的服务器下载pdf,我从API响应中获取的文件的路径如下:E:\ C1MS \ REPORTS \ 003315150418.pdf
但是,如何通过提供服务器的IP来下载此文件?
服务器的路径为:http://103.91.100.17:8080/creditOneFuelOneApp/
下载文件的代码
public static void DownloadFile(String fileURL, File directory) {
try {
FileOutputStream f = new FileOutputStream(directory);
URL u = new URL(fileURL);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = in.read(buffer)) > 0) {
f.write(buffer, 0, len1);
}
f.close();
} catch (Exception e) {
e.printStackTrace();
}
}
编辑:
http://103.91.100.17:8080/creditOneFuelOneApp/statement
请求
{
"account_number" : "000584",
"user_type" : "C"
}
响应
{
"response_code": 0,
"bill_due_date": "05-Jun-18",
"last_bill_date": "01-Jun-18",
"response_desc": "Success",
"account_name": "Credit One",
"last_bill_amount": "35083.00",
"statement_list": [
{
"bill_amount": "35083.00",
"file_location": "",
"bill_date": "01-Jun-18",
"due_date": "05-Jun-18"
},
{
"bill_amount": "35083.00",
"file_location": "E:\\C1MS\\REPORTS\\000584180501.pdf",
"bill_date": "16-May-18",
"due_date": "20-May-18"
},
{
"bill_amount": "30083.00",
"file_location": "E:\\C1MS\\REPORTS\\000584180401.pdf",
"bill_date": "01-May-18",
"due_date": "05-May-18"
},
]
请帮忙。谢谢。
答案 0 :(得分:0)
FileOutputStream f = new FileOutputStream(new File(path + File.separator
+ fileName));
您的路径是:
new StringBuilder("E:").append(File.separator)
.append("C1MS")
.append(File.separator)
.append("REPORTS").toString();
和文件名:"003315150418.pdf"
查看Oracle文档oracle fileupload
答案 1 :(得分:0)
你可以这样做。
String server_path = " http://103.91.100.17:8080/creditOneFuelOneApp/";
String server_location = "E:\C1MS\REPORTS\003315150418.pdf";
您还可以动态生成 server_location 字符串
现在像这样调用你的downloadFile函数
DownloadFile(server_path,server_location)
public static void DownloadFile(String fileURL, File directory) {
try {
FileOutputStream f = new FileOutputStream(new File(directory));
URL u = new URL(fileURL);
//YOUR OTHER CODE
}
}