導航:首頁 > 文件教程 > jsp下載文件名

jsp下載文件名

發布時間:2024-06-20 10:34:47

『壹』 jsp 下載文件路徑問題

下載文件復有兩種方式。
1.是在制你的伺服器上能相對找到。
即http://localhost8080/web 這個映射的是你伺服器上的D:\web這個目錄
那麼你這個文件就要在D:\web這個目錄中。
比如D:\web\downfile\111.xls
你的超鏈接可以這樣寫。 <a href="/downfile/111.xls">download</a>
2.就是用流的方式下載。
<a href="#" onclilck="......">download</a>
這樣的超鏈接就不是指向一個文件了,而是向伺服器提交下載申請。
這樣執行到你後台的servlet類中,你可以根據一些必要的標識知道你要下載的文件。
這樣你把D:\111.xls文件讀取出來。然後寫入到response.getOutPutStream (這個方法有些記不清了,你查一下) 這樣實現下載。

『貳』 JSP頁面用smartupload下載文件文件名英文字元亂碼問題

response.setChar..();

『叄』 JSP頁面如何保存頁面到本地

在jsp頁面抄上生成word文檔非常簡單,只需把contentType=」text/html」改為contentType="application/msword; charset=gb2312"即可,代碼如下:
<%@ page contentType="application/msword; charset=gb2312" %>
通過設置可以使原來頁面的內容在word中表現出來。
如果需要把word文檔下載下來,只需在jsp頁面上面加上如下代碼:
<%
response.setHeader("Content-Disposition", "attachment;filename=filename.doc");
%>
其中filename.doc中filename是要下載的word文檔的文件名,可以通過<%=docName%>來自行定製,如下
<%
response.setHeader("Content-Disposition", "attachment;filename=<%=docName%>.doc");
%>

『肆』 迅雷下載是.jsp文件怎麼回事

因為很多網頁並不是把文件地址放出來的,是通過JSP或者ASP等進行轉換的,你用工版具比如迅雷下載權的話由於不支持可能就直接把該件下載下來了,可以試下右鍵另存為,或者按住左鍵拖到迅雷窗口裡試下,一般都可以的。

『伍』 jsp網頁下載文件出現中文亂碼,我的下載超鏈接指向文件,如果文件名是英文,就下載正常,如果文件名是

  1. 超鏈接的地址使用js自帶的解碼函數解碼

  2. 若1還不行就在tomcat的server.xml文件里加上這個

『陸』 jsp 文件上傳和下載

1.jsp頁面
<s:form action="fileAction" namespace="/file" method="POST" enctype="multipart/form-data">
<!-- name為後台對應的參數名稱 -->
<s:file name="files" label="file1"></s:file>
<s:file name="files" label="file2"></s:file>
<s:file name="files" label="file3"></s:file>
<s:submit value="提交" id="submitBut"></s:submit>
</s:form>
2.Action
//單個文件上傳可以用 File files,String filesFileName,String filesContentType
//名稱要與jsp中的name相同(三個變數都要生成get,set)
private File[] files;
// 要以File[]變數名開頭
private String[] filesFileName;
// 要以File[]變數名開頭
private String[] filesContentType;

private ServletContext servletContext;

//Action調用的上傳文件方法
public String execute() {
ServletContext servletContext = ServletActionContext.getServletContext();
String dataDir = servletContext.getRealPath("磨脊塵/file/upload");
System.out.println(dataDir);
for (int i = 0; i < files.length; i++) {
File saveFile = new File(dataDir, filesFileName[i]);
files[i].renameTo(saveFile);
}
return "success";
}
3.配置上傳文件臨時文件夾(在struts.xml中配置)
<constant name="struts.multipart.saveDir" value="c:/temp"/>
文件下載
1.下載的url(到Action)
<a href="${pageContext.request.contextPath}/file/fileAction!down.action">下載</a>
2.struts.xml配置
<package name="file" namespace="野神/file" extends="struts-default">
<action name="fileAction" class="com.struts2.file.FileAction">
<!-- 下載文件配置 -->
<!--type 為 stream 應用 StreamResult 處理-->
<result name="down" type="stream">
<!--
不管實際類型,待下載文件 ContentType 統一指定為 application/octet-stream
默認為 text/plain
-->
<param name="contentType">application/octet-stream</param>
<!--
默認就是 inputStream,它將會指示 StreamResult 通過 inputName 屬性值的 getter 方法,
比如這里就是 getInputStream() 來獲取下載文件的內容,意味著瞎禪你的 Action 要有這個方法
-->
<param name="inputName">inputStream</param>
<!--
默認為 inline(在線打開),設置為 attachment 將會告訴瀏覽器下載該文件,filename 指定下載文
件保有存時的文件名,若未指定將會是以瀏覽的頁面名作為文件名,如以 download.action 作為文件名,
這里使用的是動態文件名,${fileName}, 它將通過 Action 的 getFileName() 獲得文件名
-->
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<!-- 輸出時緩沖區的大小 -->
<param name="bufferSize">4096</param>
</result>
</action>
</package>
3.Action
//Action調用的下載文件方法
public String down() {
return "down";
}

//獲得下載文件的內容,可以直接讀入一個物理文件或從資料庫中獲取內容
public InputStream getInputStream() throws Exception {
String dir = servletContext.getRealPath("/file/upload");
File file = new File(dir, "icon.png");
if (file.exists()) {
//下載文件
return new FileInputStream(file);

//和 Servlet 中不一樣,這里我們不需對輸出的中文轉碼為 ISO8859-1
//將內容(Struts2 文件下載測試)直接寫入文件,下載的文件名必須是文本(txt)類型
//return new ByteArrayInputStream("Struts2 文件下載測試".getBytes());
}
return null;
}

// 對於配置中的 ${fileName}, 獲得下載保存時的文件名
public String getFileName() {
String fileName ="圖標.png";
try {
// 中文文件名也是需要轉碼為 ISO8859-1,否則亂碼
return new String(fileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
return "icon.png";
}
}

『柒』 jsp實現點擊超鏈接下載文件

/** *//**
* 實現文件另存功能
*
* @param text
* 文件內容
* @param fileName
* 文件名稱
* @return
*/
protected String renderFile(String text, String fileName)
throws IOException
{
response.addHeader("Content-Disposition", "attachment; filename="
+ fileName);
response.setContentType("application/octet-stream");
response.setCharacterEncoding("GB2312");
response.getWriter().write(text);
response.flushBuffer();
response.getWriter().close();
return null;
}
下載的action:
/** *//**
* 提供下載的方法
* @return
*/
public String down()
{
String dir = getFullPath() + "/upload/file/";
try
{
if (!FileUtils.exists(dir))
{
new File(dir).mkdirs();
}
Random r = new Random(System.currentTimeMillis());
Integer randomInt = r.nextInt();
this.renderFile("test content:" + randomInt,randomInt + ".txt");
}
catch (IOException e)
{
e.printStackTrace();
this.renderText(e.getMessage());
}
return null;
}
頁面鏈接調用:
下載

閱讀全文

與jsp下載文件名相關的資料

熱點內容
編程用的鍵盤是什麼 瀏覽:316
perl判斷文件為空 瀏覽:865
java生成07版的docx 瀏覽:276
華碩a555l升級了win10 瀏覽:820
文件夾怎麼加密win7 瀏覽:341
文件及文件夾操作 瀏覽:329
大白菜win10密鑰 瀏覽:718
ghost程序是什麼 瀏覽:233
電信營業廳手機app如何測網速 瀏覽:910
邊城浪子幾個版本 瀏覽:488
更改磁碟文件系統 瀏覽:282
access2007資料庫壓縮 瀏覽:899
微信公眾號怎麼清粉 瀏覽:459
長安引力app怎麼刷u幣 瀏覽:256
windows7桌面文件夾 瀏覽:110
makefile文件格式 瀏覽:999
市面上賣的蘋果4S是真的嗎 瀏覽:946
app保存密碼 瀏覽:420
團隊網路投票介紹怎麼寫 瀏覽:891
odak音箱app在哪裡下載 瀏覽:830

友情鏈接