Java Download network files and rename __spring

Source: Internet
Author: User

File download, through the <a href= "url" > is also possible, but this direct download, the general file name is the server side of the meaningless file name.

Today, I used another one, first of all, the demand: 1. The file server and the system are not on the same server, so need to use the network address to download;

2. In order to avoid duplicate files when uploading, use UUID to generate filename, real file name stored in database;

3. All files can only be downloaded and cannot be opened directly on the browser.

According to the code to analyze: the SPRINGMVC used here

@RequestMapping ("/download") public string Downloadamachment (string DownloadURL, String realfilename,
		HttpServletRequest request, HttpServletResponse response) {Response.setcontenttype ("text/html;charset=utf-8");

			try {request.setcharacterencoding ("UTF-8");
			Bufferedinputstream bis = null;
			Bufferedoutputstream BOS = NULL;
                        The configuration file used here to remove the file server address, pieced together into a complete file server file path//demo, you can write directly http://xxx/xx/xx.txt. This form

			String Downloadpath = confighelper.getstring ("Img.server.url") + DownloadURL;
			Response.setcontenttype ("Application/octet-stream");
                        Response.reset ()//clear cache in response//create URL URL url based on network file address = new URL (downloadpath);
			
                        Gets the connection to this path URLConnection conn = Url.openconnection (); Long filelength = Conn.getcontentlengthlong ()///Get file Size//Set reponse response header, real filename rename, is set up here, set encoded res Ponse.setheader ("Content-disposition "," attachment;
			Filename= "+ New String (Realfilename.getbytes (" Utf-8 ")," iso8859-1 "));

			Response.setheader ("Content-length", String.valueof (Filelength)); bis = new Bufferedinputstream (Conn.getinputstream ());//construct read stream bos = new Bufferedoutputstream (Response.getoutputstream (
			)//construct output stream byte[] buff = new byte[1024];
			int bytesread; Every time a stream of cache size is read, write to the output stream while ( -1!= (bytesread = bis.read (buff, 0, buff.length)) {Bos.write (buff
			, 0, Bytesread);
			Response.flushbuffer ();//returns all the read streams to the client bis.close ();

		Bos.close ();
			catch (IOException e) {log.error (E.getmessage (), E);
		return errorpages._500;
	return null; }


The front page can be used with a <a href= "/download?downloadurl=xxxxx&realfilename=yyyy" > here xxxxx is the file's network address, YYY is the true and meaningful file name of a file.

This approach, mainly for different servers, can not be directly through the disk letter such as: d:/xx/xx.txt this form to build file to download. At the same time, the production system, file server in case of changes, then also need to modify the code directly, the maintenance is not high, while the solution to download the file, get the specific meaning of the file name.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.