標籤:print lis system 瀏覽器 util response java.net path 資訊
//一些主要的包和類
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.net.URLDecoder;
import java.net.URLEncoder;
/**
* 下載檔案
* @return
* @throws UnsupportedEncodingException
*/
@Action(value="downloadAttachFile")
public String downloadAttachFile(){
//從前台傳過來的附件名字:格式是,“2006#系統測試.docx格式"
String downloadName =wpyId+"#"+attachName;
//傳入的資料
System.out.println(downloadName);
try {
OutputStream os = this.response.getOutputStream();
//OutputStream os = this.getResponse().getOutputStream();
if (Util.isIE(this.request)) //判斷用戶端是否為IE
{
//編碼
downloadName = URLEncoder.encode(downloadName, "UTF-8");
}
else
{
downloadName = new String(downloadName.getBytes("UTF-8"),"iso-8859-1");
}
//設定讓瀏覽器彈出下載對話方塊的Header
this.response.setContentType("application/x-download");
this.response.addHeader("Content-Disposition","attachment;filename=\"" + downloadName + "\"");
this.response.flushBuffer();
//將前台的資料經過封裝後(加了頭資料,並經過編碼)需要解碼
String downloadName1 =URLDecoder.decode(downloadName, "UTF-8");
System.out.println(downloadName1);
//解碼的結果輸出
String bgFile=realPath + File.separator + downloadName1;
FileInputStream fis = new FileInputStream(bgFile);
Util.copyStrem(fis, os);
fis.close();
os.close();
System.out.println("==========成功了!!===========");
} catch (IOException e) {
e.printStackTrace();//在命令列列印異常資訊在程式中出錯的位置及原因
System.out.println("==========出錯了!!===========");
}
return NONE;
}
jsp action中附件下載的寫法