1. The most direct and simple way is to put the file address directly into a link on the html page. The disadvantage of this is that the path of the file on the server is exposed, and other controls (such as permissions) on file downloads cannot be performed. I won’t write an example here.
2. On the server side, the file is converted into an output stream, written to the response, and the file is brought to the browser with the response. The browser prompts the user whether he is willing to save the file locally. (example below)
<%
response.setContentType(fileminitype);
response.setHeader("Location",filename);
response.setHeader("Cache-Control", "max-age=" + cacheTime);
response.setHeader("Content-Disposition", "attachment; filename=" + filename); //filename should be encoded (utf-8)
response.setContentLength(filelength);
OutputStream outputStream = response.getOutputStream();
InputStream inputStream = new FileInputStream(filepath);
byte[] buffer = new byte[1024];
int i = -1;
while ((i = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, i);
}
outputStream.flush();
outputStream.close();
inputStream.close();
outputStream = null;
%>
3. Since it is JSP, another way is to use Applet to download files. However, the customer must first trust your Applet applet, which will accept the data stream sent by the servlet and write it locally.
servlet side example
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType(" text/plain ");
OutputStream outputStream = null;
try {
outputStream = res.getOutputStream();
popFile(srcFile, outputStream));//Write the file with the file path srcFile into the outputStream.
} catch (IOException e) {
e.printStackTrace();
}
}
JApplet side example
URLConnection con;
try {
con = url.openConnection();//url is the URL of the called SERVLET such as http://localhost:8080/sendDateSevlet.do
con.setUseCaches(false);
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Content-Type",
"application/octet-stream");
InputStream in = con.getInputStream();
ProgressMonitorInputStream pmInputStream = new ProgressMonitorInputStream(
pane, "Downloading file content from server", in);
ProgressMonitor pMonitor = pmInputStream
.getProgressMonitor();
pMonitor.setMillisToDecideToPopup(3);
pMonitor.setMillisToPopup(3);
String localfilepath = localstr + filename;//localfilepath local path, localstr file folder, filename local file name
if(saveFilsaveFilee(localfilepath,pmInputStream)){ //The method saveFilsaveFilee writes the input stream pmInputStream to the file localfilepath.
openLocalFile(localfilepath);
}
4. By the way, the code for JApplet to upload files is also posted.
JApplet side example
URLConnection con;
try {
con = url.openConnection();//url is the URL of the called SERVLET such as http://localhost:8080/sendDateSevlet.do
con.setUseCaches(false);
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Content-Type",
"application/octet-stream");
OutputStream out = con.getOutputStream();
String localfilepath = localstr + filename; //localfilepath local path, localstr file folder, filename local file name
getOutputStream(localfilepath,out);//The file getOutputStream writes the file localfilepath to the output stream out.
InputStream in = con.getInputStream();
return true;
}catch (IOException e) {
System.out.println("File upload error!");
e.printStackTrace();
}
servlet side code example
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType(" text/plain ");
InputStream inputStream = null;
try {
inputStream = res.getInputStream();
writefile(srcFile, inputStream);//Save the input stream inputStream to the file with the file path srcFile
} catch (IOException e) {
e.printStackTrace();
}
} // end service
summary: In file transmission, it exists in the form of a stream, and on the hard disk, it exists in the form of a file. All we have to do is send the stream and read the stream through HttpServletRequest and HttpServletResponse, or response and request. As well as operations that convert files to streams or streams to files.