无法使用java下载文件

时间:2013-01-15 13:16:59

标签: java upload download

我刚刚实现了一个jsp程序,用于将文件上传到我的PC文件夹中。此文件夹的路径为E:\UploadedFile。我要下载的文件名是assad.xml(我刚刚上传的文件名)。这就是我试图下载它的方式。如果我错了,请检查我的代码并纠正我。

<%@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 。它在此文件夹中可用。

1 个答案:

答案 0 :(得分:0)

正如Renjith所说,你可能会颠倒文件名。有关更一致的API,请查看:http://hc.apache.org/httpcomponents-core-ga/index.html