JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Servlet/JSP 討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 [分享] 強制存檔的方法 [精華]
jog

Solaris 才是王道阿!!



發文: 76
積分: 3
於 2003-09-30 00:35 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
之前遇到的問題...
就是 Server side 產生一個文字檔
那個文字檔是一些系統資訊
但是那些文字檔必須讓 user 按右鍵存檔
若不按右鍵就會從 browser 秀出來
還要教育 user 按右鍵存檔
相當的麻煩

以下是解決方式...還能夠替 user 指定預設檔名相當方便
fname 即是檔名
contentType, 自行決定

response.setContentType(contentType);
response.setHeader("Content-Disposition","attachment;filename=\""+fname+"\"");

檔案的內容就是 write out 內容
所以也可以寫 binary 進去
例如畫完圖/或產生文字檔...
可讓設定檔名讓 user 存..不會直接秀在網頁上


reply to postreply to post
作者 Re:[分享] 強制存檔的方法 [Re:jog]
annhy

來呦~



發文: 45
積分: 2
於 2003-10-24 17:52 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
非常謝謝你!!

可是我在測試的時候有件奇怪的事情,
就是 filename 用英文字母就沒問題,可是中文檔名就無法成功

比如說我的程式這樣寫是 OK 的:
1
2
response.setContentType("application/vnd.ms-word");
response.setHeader("Content-Disposition","attachment;filename=\"abc123.doc\"");


但是這樣就不行了:
1
2
response.setContentType("application/vnd.ms-word");
response.setHeader("Content-Disposition","attachment;filename=\"我是中文.doc\"");


結果執行時,原來的中文檔名卻變成一串隨機(每次點選時都不一樣)的英文字串,畫面如附件。

我想一定是我哪裡的設定有問題,可是找了好久,就是看不出來...


annhy edited on 2003-10-24 17:58
reply to postreply to post
作者 Re:[分享] 強制存檔的方法 [Re:jog]
annhy

來呦~



發文: 45
積分: 2
於 2003-10-24 18:04 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
剛剛發現 JSPtw 所使用的 Jute 程式也有相同的問題。

各位看官可以試著在文章中加上一個中文檔名的附件,然後對它另存新檔。
我發現它的檔名比我的程式還慘,是一堆不知道什麼 encoding 的字...


reply to postreply to post
作者 Re:[分享] 強制存檔的方法 [Re:annhy]
annhy

來呦~



發文: 45
積分: 2
於 2003-10-28 09:42 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
自問自答...
要設定地區才會正確...

原來只是這樣的問題... 我還真遜...

1
2
3
response.setLocale(java.util.Locale.TAIWAN);  // 要設定地區才會正確...
response.setContentType("application/vnd.ms-word");
response.setHeader("Content-Disposition","attachment;filename=\"我是中文.doc\"");


annhy edited on 2003-10-28 11:49
reply to postreply to post
作者 Re:[分享] 強制存檔的方法 [Re:annhy]
leohong





發文: 125
積分: 1
於 2003-11-04 18:03 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Dear :
我也遇到相同的問題,我依照上述作法,但是當 browser 出現下載視窗時,所有的中文檔名都變成亂碼

Web Server : WebLogic 8.1
Browser : IE 5.5


reply to postreply to post
作者 Re:[分享] 強制存檔的方法 [Re:annhy]
duba





發文: 10
積分: 1
於 2003-11-19 18:06 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請問正確的下載應該怎樣寫法?
我把上面那段加到我下載連結的上方
結果是整個jsp檔案下載  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bufstr = rs.getString(2);
if(bufstr==null || bufstr.length()==0)
 out.println("<font face='Arial, Helvetica, sans-serif'>-</font>");
                      else
                      {
                    
  response.setLocale(java.util.Locale.TAIWAN);  
  response.setContentType("application/pdf");
  response.setHeader("Content-Disposition","attachment;filename=\""+bufstr+"\"");
 
                         //request.setCharacterEncoding("utf-8");
 
                   %>
//以下是我要下載的連結
 <a href="files/<%=java.net.URLEncoder.encode(bufstr,"uft8")%>" ><img src="img/index/d.gif" width="41" height="15" border="0"></a>


browser edited on 2003-11-19 23:12
reply to postreply to post
作者 Re:[分享] 強制存檔的方法 [Re:annhy]
klmer





發文: 2
積分: 0
於 2004-03-30 14:41 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
annhy wrote:
自問自答...
要設定地區才會正確...

原來只是這樣的問題... 我還真遜...

1
2
3
response.setLocale(java.util.Locale.TAIWAN);  // 要設定地區才會正確...
response.setContentType("application/vnd.ms-word");
response.setHeader("Content-Disposition","attachment;filename=\"我是中文.doc\"");



我在Tomcat 4.0.3 測試沒有問題...
但是Tomcat 換成 4.1.30 一樣的程式 中文還是不行....
Q_Q


reply to postreply to post
作者 Re:[分享] 強制存檔的方法 [Re:klmer]
hugoyu





發文: 16
積分: 0
於 2004-03-30 16:52 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
klmer wrote:
我在Tomcat 4.0.3 測試沒有問題...
但是Tomcat 換成 4.1.30 一樣的程式 中文還是不行....
Q_Q


要不要試試看這樣寫:

1
2
response.setContentType("application/vnd.ms-word");
response.setHeader("Content-disposition", "attachment; filename=" + URIUtil.encodeQuery("我是中文.doc","utf-8"));


用這個 class: org.apache.commons.httpclient.util.URIUtil


hugoyu edited on 2004-03-30 16:55
reply to postreply to post
作者 Re:[分享] 強制存檔的方法 [Re:jog]
rainman





發文: 67
積分: 1
於 2004-03-31 09:51 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list


強制存檔的方法.zip (167.52k)


reply to postreply to post
作者 Re:[分享] 強制存檔的方法 [Re:annhy]
chenghuan





發文: 48
積分: 0
於 2004-04-26 20:36 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我用hugoyu大大的方式,IE6是OK的,可是IE5還是會變亂碼Sad,怎麼辦呢?

reply to postreply to post
作者 Re:[分享] 強制存檔的方法 [Re:jog]
ahc





發文: 26
積分: 0
於 2005-02-16 14:32 user profilesend a private message to usersend email to ahcreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我是用
fileName = java.net.URLEncoder.encode(fileName, "utf-8");

httpServletResponse.setHeader("Content-Disposition",
"attachment; filename=" + fileName );


reply to postreply to post
哇細阿曦
作者 Re:[分享] 強制存檔的方法 [Re:jog]
enjoyit





發文: 5
積分: 0
於 2005-03-10 16:30 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
I met another question, can any one help me?

response.setContentType("application/vnd.ms-word");response.setHeader("Content-Disposition","attachment;filename=\"abc123.tar.gz\"");

Ie browser will automatic rename the archive as
abc123.tar.gz[1]


reply to postreply to post
» JWorld@TW »  Servlet/JSP 討論區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8