Four Methods of JSP File Download Function

  • 2021-07-18 08:46:59
  • OfStack

For the website, the website itself often needs to provide some resources or information for downloading. When it comes to downloading, the most primitive method is to provide the downloaded website address on the web page. Today, there are several other ways to realize file download. It depends on your own needs which way is better.

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 there is no other control (such as permissions) over file downloads. I won't write an example for this.
2. Convert the file into output stream on the server side, write it to response, take the file to the browser with response, and the browser will prompt the user whether he is willing to save the file locally. Examples are as follows:


<%
response.setContentType(fileminitype);
response.setHeader("Location",filename);
response.setHeader("Cache-Control", "max-age=" + cacheTime);
//filename It should be coded (utf-8)
response.setHeader("Content-Disposition", "attachment; filename=" + filename); 
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, there is another way to download files with Applet. However, customers must first trust your Applet applet, which accepts the data stream sent by servlet and writes it locally.

servlet End Example


public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType(" text/plain ");
OutputStream outputStream = null;
try {
outputStream = res.getOutputStream();
// Set the file path to srcFile Write to the file of outputStream Medium 
popFile(srcFile, outputStream)) ;
} catch (IOException e) {
e.printStackTrace(); 
}
}

JApplet End Example


URLConnection con;
try {
//url Is the invoked SERVLET Web site of   Such as  *.do 
con = url.openConnection();
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 contents from server ", in);
ProgressMonitor pMonitor = pmInputStream.getProgressMonitor();
pMonitor.setMillisToDecideToPopup(3);
pMonitor.setMillisToPopup(3);
//localfilepath Local path ,localstr Files folder, filename Local file name 
String localfilepath = localstr + filename ;
// Method saveFilsaveFilee Is to put the input stream pmInputStream Write to file localfilepath Medium  
if(saveFilsaveFilee(localfilepath,pmInputStream)){
openLocalFile(localfilepath);
}

4. By the way, post the code of JApplet uploading file.

JApplet End Sample


URLConnection con;
try {
con = url.openConnection();
//url Is the invoked SERVLET Web site of   Such as  *.do 
con.setUseCaches(false);
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Content-Type","application/octet-stream"); 
OutputStream out = con.getOutputStream();
//localfilepath Local path ,localstr Files folder, filename Local file name 
String localfilepath = localstr + filename;
// Documents getOutputStream Is to put the document localfilepath Write to output stream out Medium 
getOutputStream(localfilepath,out);
InputStream in = con.getInputStream();
return true;
}catch (IOException e) {
System.out.println(" File upload error! ");
e.printStackTrace();
}

servlet End Code Sample


public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType(" text/plain ");
InputStream inputStream = null;
try {
inputStream = res.getInputStream();
// Put the input stream inputStream Save to file path is srcFile In the file of 
writefile(srcFile, inputStream);
} catch (IOException e) {
e.printStackTrace();
}
} // end service

Summary: It exists in the form of stream in file transmission and in the form of file on hard disk. All we have to do is send and read streams through HttpServletRequest and HttpServletResponse, or response and request. And the operation of converting files into streams or streams into files.

The above is the method of JSP file download function. I hope there is a method that can suit you and help everyone solve the implementation problem of JSP file download function.


Related articles: