Java file downloads in several ways, as follows:
Public httpservletresponse Download (String path, httpservletresponse response) {try {//path is the path to the file you want to download.
File File = new file (path);
Gets the file name.
String filename = File.getname ();
Gets the suffix name of the file.
String ext = filename.substring (Filename.lastindexof (".") + 1). toUpperCase ();
Download the file as a stream.
InputStream fis = new Bufferedinputstream (new FileInputStream (path));
byte[] buffer = new byte[fis.available ()];
Fis.read (buffer);
Fis.close ();
Empty response Response.reset (); Sets the header Response.AddHeader of the response ("Content-disposition", "attachment;filename=" + New String (filename.getbytes
()));
Response.AddHeader ("Content-length", "" "+ File.length ());
OutputStream toclient = new Bufferedoutputstream (Response.getoutputstream ());
Response.setcontenttype ("Application/octet-stream");
Toclient.write (buffer);
Toclient.flush ();
Toclient.close ();
catch (IOException ex) {Ex.printstacktrace ();
return response; public void downloadlocal (HttpServletResponse response) throws FileNotFoundException {//download local file String filen Ame = "Operator.doc". toString (); Default save name//read to stream InputStream instream = new FileInputStream ("C:/operator.doc");//file storage path//format of output re
Sponse.reset ();
Response.setcontenttype ("bin"); Response.AddHeader ("Content-disposition", "attachment;
Filename=\ "" + fileName + "\");
Loop out the data in the stream byte[] b = new byte[100];
int Len;
try {while (len = Instream.read (b)) > 0) response.getoutputstream (). Write (b, 0, Len);
Instream.close ();
catch (IOException e) {e.printstacktrace (); } public void Downloadnet (HttpServletResponse response) throws malformedurlexception {//download network file int Bytesu
m = 0;
int byteread = 0;
URL url = new URL ("Windine.blogdriver.com/logo.gif");
try {URLConnection conn = url.openconnection (); InputStream instream = Conn.getinputstream ();
FileOutputStream fs = new FileOutputStream ("C:/abc.gif");
byte[] buffer = new byte[1204];
int length;
while ((Byteread = instream.read (buffer))!=-1) {bytesum + = Byteread;
System.out.println (bytesum);
Fs.write (buffer, 0, byteread);
} catch (FileNotFoundException e) {e.printstacktrace ();
catch (IOException e) {e.printstacktrace ();
}
}
//one way to support open files online
public void DownLoad (String filePath, HttpServletResponse response, Boolean isonline) throws Exception {File F = new
File (FilePath);
if (!f.exists ()) {Response.senderror (404, "File not found!");
Return
Bufferedinputstream br = new Bufferedinputstream (new FileInputStream (f));
byte[] buf = new byte[1024];
int len = 0; Response.reset ();
Very important if (Isonline) {//Online open way url u = new URL ("file:///" + FilePath);
Response.setcontenttype (U.openconnection (). getContentType ()); Response.setheader ("content-disposition", "inline;
Filename= "+ f.getname ());
The filename should be encoded as UTF-8} else {//Pure download mode Response.setcontenttype ("Application/x-msdownload"); Response.setheader ("Content-disposition", "attachment;
Filename= "+ f.getname ());
} OutputStream out = Response.getoutputstream ();
while (len = Br.read (buf)) > 0) out.write (buf, 0, Len);
Br.close ();
Out.close ();
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.