我剛剛實現了一個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。它在此文件夾內可用。
new FileOutputStream(「E:/assad.xml」);或者E:\ UploadedFile \ assad.xml? – Renjith
怎麼回事? –
文件上傳代碼運行良好。但上面的代碼下載文件不能解決我的問題。 –