If you want to set a URL to download a file, here's how
You need to set the file response type, using Response.setcontenttype, such as a JPEG format image. If you want to access the page when you download a saved window, use the Response.setheader ("Content-disposition", "Attachment;filename=" +filename.getname ()) function. It can also be a PDF file, or other format, if you want to find out what format the browser can recognize the file, you need to look in the server's configuration file, such as the Tomcat server search mime-mapping can see the browser is another file type.
Public voiddoget (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException { Response.setcharacterencoding ("Utf-8"); File filename=NewFile ("d:\\chengxu\\ml\\2.jpg"); Response.setcontenttype ("Image/jpeg"); Response.setheader ("content-disposition","attachment;filename="+filename.getname ()); OutputStream out=Response.getoutputstream (); FileInputStream Fileinput=Newfileinputstream (filename); byte[] Charbuffer =New byte[1024x768]; intLength =0; while((Length=fileinput.read (charbuffer))!=-1) { out. Write (Charbuffer,0, length); } }
How to set up a download file in Java