2009-06-01

中文檔案下載

編碼的問題
只要是編碼就是大問題
很難解決 欲哭無淚 傷透腦筋 差點睡著
卡了我一整個下午
最後終於在javaworld 找到答案 爽! javaworld文章
要先準備3個檔案
commons-codec-1.3.jar
commons-httpclient-3.0.1.jar
commons-logging-1.1.jar

這很好找 辜狗問一下就有官網可以下載



import org.apache.commons.httpclient.util.URIUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

String Filename = "xxx"; //下載時的檔案名稱
String FileSrc = "yyy"; //檔案在硬碟的路徑
try{
response.setHeader("Content-disposition","attachment; filename=" + URIUtil.encodeQuery(Filename));
} catch (URIException e){
out.println ("error = " + e.toString());
}

FileInputStream fis = new FileInputStream( filesrc );

OutputStream os=response.getOutputStream();
int byteRead;
while(-1 != (byteRead = fis.read()))
os.write(byteRead);
os.close();
if (fis != null) {
response.setStatus(response.SC_OK);
response.flushBuffer();
fis.close();
}

沒有留言:

張貼留言

有問題就留下吧,盡力而為回答。不要留垃圾訊息就好!