當前位置:編程學習大全網 - 源碼下載 - web開發資源上傳下載代碼怎麽寫?有什麽好的插件可以用?項目缺少資源下載這個模塊,後臺java前臺jsp

web開發資源上傳下載代碼怎麽寫?有什麽好的插件可以用?項目缺少資源下載這個模塊,後臺java前臺jsp

struts 有提供的。

文件上傳

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";

}

}

  • 上一篇:11款能提高工作效率的辦公小程序,妳知道嗎?
  • 下一篇:小程序源代碼時代
  • copyright 2024編程學習大全網