2013-01-15 126 views
-3

我剛剛實現了一個jsp程序,將文件上傳到我的PC文件夾中。此文件夾的路徑是E:\UploadedFile。我想下載的文件名是assad.xml(我剛剛上傳的文件名)。這是我如何下載它。請檢查我的代碼並糾正我,如果我錯了。無法使用java下載文件

<%@page import="java.io.*,java.net.*"%> 


<% 
    System.setProperty("http.proxyHost", "192.168.1.10"); 
    System.setProperty("http.proxyPort", "8080"); 
try { 
    /* 
    * Get a connection to the URL and start up 
    * a buffered reader. 
    */ 
    long startTime = System.currentTimeMillis(); 
    System.out.println("Connecting to URL...\n"); 
    URL url = new URL("E://UploadedFiles/"); 
    url.openConnection(); 
    InputStream reader = url.openStream(); 
    /* 
    * Setup a buffered file writer to write 
    * out what we read from the website. 

    */ 
    FileOutputStream writer = new FileOutputStream("E:/assad.xml"); 
    byte[] buffer = new byte[153600]; // Buffer for 150K blocks at a time 
    int totalBytesRead = 0; 
    int bytesRead = 0; 
    System.out.println("Reading ZIP file 150KB blocks at a time.\n"); 

    while ((bytesRead = reader.read(buffer)) > 0){ 
     writer.write(buffer, 0, bytesRead); 
     buffer = new byte[153600]; 
     totalBytesRead += bytesRead; 
    } 
    long endTime = System.currentTimeMillis(); 

    System.out.println("Done. " + (new Integer(totalBytesRead).toString()) + " bytes 
    read (" + 
      (new Long(endTime - startTime).toString())+ " millseconds).\n"); 
    writer.close(); 
    reader.close(); 
}catch (MalformedURLException e){ 
    e.printStackTrace(); 
} 
catch (IOException e){ 
    e.printStackTrace(); 
} 

%> 

在我剛剛上傳我的文件的文件夾的名稱是UPLOADEDFILES及其E:盤。我想下載的文件的名稱是assad.xml。它在此文件夾內可用。

+0

new FileOutputStream(「E:/assad.xml」);或者E:\ UploadedFile \ assad.xml? – Renjith

+0

怎麼回事? –

+0

文件上傳代碼運行良好。但上面的代碼下載文件不能解決我的問題。 –

回答

0

可能你倒過來了,就像Renjith說的那樣。要獲得更一致的API,請參閱:http://hc.apache.org/httpcomponents-core-ga/index.html

+0

Item Jr ...我只想在上面的代碼中進行一些修改。它可以在我從互聯網上下載任何東西時起作用但它沒有下載E:\ UploadedFiles \ assad.xml中可用的數據在我的PC –

+0

謝謝。我已經想通了自己 –