本文目錄一覽:
如何下載jsp文件?
一般情況下不會,一般的web程序都會控制這方面的下載,除非有bug或者是用其它一些軟體
JSP通過超鏈接下載文件
JSP頁面點擊超鏈接彈出文件下載,代碼如下:
%
String path = request.getContextPath();
String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
%
//然後
a href =”%= basePath %/upload/aa.doc }” target=”_blank”下nbsp;nbsp;載/a
註:%= basePath %獲取部署JSP項目的根目錄,/upload/aa.doc/是根目錄uploadaa.doc文件,根據需求修改即可。
如何把網站上的一個jsp文件下載下來
你只能下載頁面上的代碼 ,不能下載後台的代碼的!後台在人家伺服器上!你是通過客戶端向伺服器傳送數據,在人家伺服器上處理的!所以你只能下載前端的東西!下載下來也就是了靜態的網頁
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實現文件的下載
%@pagelanguage=”java” import=”java.io.*,java.net.*” contentType=”application/x-msdownload” pageEncoding=”UTF-8″%%
//關於文件下載時採用文件流輸出的方式處理:
//加上response.reset(),並且所有的%後面不要換行,包括最後一個;
String url = request.getParameter(“url”);
System.out.print(url);
int k = url.lastIndexOf(“\\”);
String url1=url.substring(k+1,url.length());
response.reset();//可以加也可以不加
response.setContentType(“application/x-download”);
String filedownload = url;
String filedisplay = url1;
filedisplay = URLEncoder.encode(filedisplay,”UTF-8″);
response.addHeader(“Content-Disposition”,”attachment;filename=” + filedisplay);
OutputStream outp = null;
FileInputStream in = null;
try
{
outp = response.getOutputStream();
in = new FileInputStream(filedownload);
byte[] b = new byte[1024];
int i = 0;
while((i = in.read(b)) 0)
{
outp.write(b, 0, i);
}
out.clear();
out = pageContext.pushBody();
outp.flush();
}
catch(Exception e)
{
System.out.println(“Error!”);
}
finally
{
if(in != null)
{
in.close();
in = null;
}
if(outp != null)
{
outp.close();
outp = null;
}
}
%
原創文章,作者:WPGHU,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/316086.html