从URL下载附件文件 - JAVA

时间:2017-07-26 11:07:42

标签: java file file-io io httpconnection

我正在尝试从以下网址下载文件。 http://localhost/attachment.php?attachId=123 它是作为PHP文件而不是附件下载的。我试过这样的。

url = new URL(sourceURL);
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection(); 
httpcon.addRequestProperty("User-Agent", "Mozilla/4.76");
if(httpcon.getResponseCode()!=404){
    InputStream is = httpcon.getInputStream();
    Files.copy(is, Paths.get(destinationFile));
}

0 个答案:

没有答案