Java File Download Tool class __util

Source: Internet
Author: User
/** * Desc: Download Tool class * @date 2014-6-26/public class Downloadutil {/** * Desc: According to the file header to determine the request from the browser, so that the targeted file name transcoding * @dat E 2014-6-26 * @param request * @return * @throws Exception/public static string Transfilename (String fileName  
		, HttpServletRequest request) {String Agent = Request.getheader ("user-agent");		FileName = Filename.trim (); 
				Go to the end of the space try {////According to the file header to judge the request from the browser so that the targeted file name transcoding if (null!= agent &&-1!= agent.indexof ("TheWorld")) {//window of the world 	FileName = new String (filename.getbytes ("gb2312"), "iso8859-1"); Resolves the downloaded file name containing the parentheses convert%28%29}else if (null!= agent &&-1!= agent.indexof ("MSIE 8.0")) {//IE8 String lenfile
				Name = Urlencoder.encode (FileName, "UTF-8"); if (Lenfilename.length () > 150) {//filename length is greater than 150 characters filename = new String (filename.getbytes ("gb2312"), "Iso8859-1" 
				);
				else {filename = urlencoder.encode (filename, "UTF-8"). Replace ("+", "%20"); }}else if (null!= agent &&-1!= Agent.indexoF ("MSIE 7.0") &&-1!= agent.indexof ("SE 2.X METASR 1.0")) {//sogo Browser filename = urlencoder.encode (filename, "U
			TF-8 "). Replace (" + ","%20 "); }else if (null!= agent && ( -1!= agent.indexof ("SV1") | |-1!= agent.indexof ("360SE")) {//360 Secure browser String len
				filename = urlencoder.encode (filename, "UTF-8"); if (Lenfilename.length () > 150) {//filename length is greater than 150 characters filename = new String (filename.getbytes ("gb2312"), "Iso8859-1" 
				);
				else {filename = urlencoder.encode (filename, "UTF-8"). Replace ("+", "%20"); }}else if (null!= agent &&-1!= agent.indexof ("Chrome") {//google fileName = new String (filename.getbyte 	S ("gb2312"), "iso8859-1"); The file name of the resolution download contains the parentheses conversion%28%29}else if (null!= agent &&-1!= agent.indexof ("Firefox")) {//firefox filename = 
			New String (Filename.getbytes ("gb2312"), "iso8859-1"); }else if (null!= agent && 1!= agent.indexof ("Safari")) {//firefox fileName = new String (Filename.getbytes ("U TF-8 "), "iso8859-1"); 
			}else {//other browser fileName = new String (filename.getbytes ("gb2312"), "iso8859-1");
		The catch (Exception e) {throw new RuntimeException ("Transfilename error", E);
	return fileName;
 }
}

Related Article

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.