本文目錄一覽:
我想用jsp語言編寫的網頁中實現文件上傳、下載的功能,請問完整的代碼怎麼寫,盡量是最簡單的,謝謝
input type=’file’
然後設置form的 enctype= “multipart/form-data “
然後後台從HttpServletRequest裡面取文件出來就ok了!
用jsp怎麼編寫文件下載代碼
下面是我寫的一個小例子,下載遠程文件urlString,到本地文件localFile.
成功返回True,不成功返回False.
把這代碼插入到你JSP中用到的地方就OK了:)
public boolean downLoadFile(String urlString, String localFile) {
URL url;
byte[] buffer = new byte[512];
int size = 0;
boolean success = false;
try {
url = new URL(urlString);
BufferedInputStream stream = new BufferedInputStream(url.openStream());
FileOutputStream fos = new FileOutputStream(localFile);
while ((size = stream.read(buffer)) != -1) {
fos.write(buffer, 0, size);
}
fos.close();
stream.close();
success = true;
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return success;
}
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通過超鏈接下載文件
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文件,根據需求修改即可。
原創文章,作者:VZPZ,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/144191.html