天天看点

JSP实现文件下载同时解决中文乱码问题

杞浇聽 JSP瀹炵幇鏂囦欢涓嬭浇涓庝腑鏂囦贡鐮侀棶棰樿В鍐?/h3>

聽涔嬪墠,鍐欒繃涓€涓狣ownload.jsp鏂囦欢,鍙互瑙e喅涓嬭浇鏂囦欢涔辩爜闂(璇稿:DOC,XSL鏂囦欢绛夌瓑).

鍚庢潵鍙戠幇,閬囧埌涓枃鍚嶇殑鏂囦欢鐨勬椂鍊?鏂囦欢涓嬭浇灏嗕細鎶ラ敊~~~~

浠婂ぉ,閫氳繃鏀瑰啓鍘烡ownload.jsp鏂囦欢宸茬粡褰诲簳瑙e喅浜嗚繖涓棶棰榽

鐜板湪,鎶婁竴鏁村鐨勬枃浠朵笂浼犱笅杞界殑鏂规硶缁欒创鍑烘潵~~~浠ヤ究澶у鍊熼壌!~!~!~!~!聽

浣滆€?鍙ゅ焹鍙婃硶鑰?

download.jsp鏂囦欢

---------------------------------------------------------

<%

聽聽java.io.BufferedInputStream聽bis = null ;

聽聽java.io.BufferedOutputStream聽聽bos = null ;

try{

聽 String 聽filename = request.getParameter( " filename " );

聽聽聽聽聽聽聽聽聽聽聽聽聽filename = new 聽 String (filename.getBytes( " iso8859-1 " ), " gb2312 " );

聽response.setContentType( " application/x-msdownload " );

聽response.setHeader( " Content-disposition " , " attachment;聽filename= " + new 聽 String (filename.getBytes( " gb2312 " ), " iso8859-1 " ));

聽bis聽 = new 聽java.io.BufferedInputStream( new 聽java.io.FileInputStream(config.getServletContext().getRealPath( " files/ " 聽 + 聽filename)));

聽bos = new 聽java.io.BufferedOutputStream(response.getOutputStream());聽

聽 byte []聽buff聽 = 聽 new 聽 byte [ 2048 ];

聽 int 聽bytesRead;

聽 while ( - 1 聽! = 聽(bytesRead聽 = 聽bis.read(buff,聽 0 ,聽buff.length)))聽{

聽聽bos.write(buff, 0 ,bytesRead);

聽}

}

catch(Exception聽e){

聽e.printStackTrace();

}

finally聽{

聽 if 聽(bis聽! = 聽 null )bis.close();

聽 if 聽(bos聽! = 聽 null )bos.close();

}

%> 聽

娉ㄦ剰锛屽叧閿氨鏄痵etHeader閲岀殑filename闇€瑕侀噸鏂扮紪鐮?鏍煎紡鏄疘SO-8859-1灏憋集锛簡

浠ヤ笅鏄垜鑷繁椤圭洰涓敤鍒扮殑浠g爜鐗囨柇锛屼緵鍙傝€?

list.jsp: 鏄剧ず闄勪欢鍚嶇О鐨勯〉闈?

< tr >

聽聽聽聽聽聽聽聽聽聽聽聽 < td聽 height ="25" 聽class ="tdcor" > 闄?&nbsp;&nbsp; 浠?&nbsp; </ td >

聽聽聽聽聽聽聽聽聽聽聽聽 < td聽 colspan ="3" 聽height =50 >

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 <%

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 if 聽( null 聽! = 聽publish.getAttatchFilename()聽 &&

publish.getAttatchFilename().length()聽 > 聽 0 )聽{

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 %>

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 < a聽 href ="publish_do.jsp?method=download&fileName=

<%=URLEncoder.encode(publish.getAttatchFilename()," GBK")% > ">

<% = URLDecoder.decode(publish.getAttatchFilename(), " GBK " ) %> </ a >

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 <%

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 %>

聽聽聽聽聽聽聽聽聽聽聽聽 </ td >

</ tr >

download.jsp:涓嬭浇椤甸潰

else 聽 if 聽( null 聽 != 聽method聽 && 聽method.equals( " download " ))聽{ // 涓嬭浇闄勪欢

聽聽聽聽聽聽聽聽String聽fileName聽 = 聽request.getParameter( " fileName " );

聽聽聽聽聽聽聽聽File聽file聽 = 聽 new 聽File(Constants.PUBLISH_FILE_PATH聽 + 聽 " / " 聽 + 聽URLDecoder.decode(fileName, " GBK " ));

聽聽聽聽聽聽聽聽response.reset();

聽聽聽聽聽聽聽聽response.setContentType( " application/octet-stream;聽charset=GBK " );

聽聽聽聽聽聽聽聽response.addHeader( " Content-Disposition " ,聽 " attachment;聽filename= " 聽 + 聽CourseDetailBusiness.transfer(URLDecoder.decode(fileName, " GBK " ), " GBK " , " ISO-8859-1 " ));

聽聽聽聽聽聽聽聽response.setContentLength(( int )聽file.length());

聽聽聽聽聽聽聽聽 byte []聽buffer聽 = 聽 new 聽 byte [ 4096 ];

聽聽聽聽聽聽聽聽BufferedOutputStream聽output聽 = 聽 null ;

聽聽聽聽聽聽聽聽BufferedInputStream聽input聽 = 聽 null ;

聽聽聽聽聽聽聽聽 // 聽鍐欑紦鍐插尯锛?

聽聽聽聽聽聽聽聽 try 聽{

聽聽聽聽聽聽聽聽聽聽聽聽output聽 = 聽 new 聽BufferedOutputStream(response.getOutputStream());

聽聽聽聽聽聽聽聽聽聽聽聽input聽 = 聽 new 聽BufferedInputStream( new 聽FileInputStream(file));

聽聽聽聽聽聽聽聽聽聽聽聽 int 聽n聽 = 聽( - 1 );

聽聽聽聽聽聽聽聽聽聽聽聽 while 聽((n聽 = 聽input.read(buffer,聽 0 ,聽 4096 ))聽 > 聽 - 1 )聽{

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽output.write(buffer,聽 0 ,聽n);

聽聽聽聽聽聽聽聽聽聽聽聽}

聽聽聽聽聽聽聽聽聽聽聽聽response.flushBuffer();

聽聽聽聽聽聽聽聽}

聽聽聽聽聽聽聽聽 catch 聽(Exception聽e)聽{

聽聽聽聽聽聽聽聽}聽 // 聽maybe聽user聽cancelled聽download

聽聽聽聽聽聽聽聽 finally 聽{

聽聽聽聽聽聽聽聽聽聽聽聽 if 聽(input聽 != 聽 null )聽input.close();

聽聽聽聽聽聽聽聽聽聽聽聽 if 聽(output聽 != 聽 null )聽output.close();

聽聽聽聽聽聽聽聽}

璇存槑:

锛戙€傛枃浠跺悕鍦ㄦ暟鎹簱涓繚瀛樼殑缂栫爜涓篣RLEncode

2.鍦╨ist.jsp鏄剧ず鐨勬椂鍊欏浜嗕竴娆ncode,涓嶇煡涓轰粈涔堬紝涓峞ncode涓€娆¤繕涓嶈锛屽疄闄呬笂鏄浜屾缂栫爜浜?