【struts2】文件上传下载—struts2实现
生活随笔
收集整理的這篇文章主要介紹了
【struts2】文件上传下载—struts2实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
struts2實現上傳下載時要用到的jar包:
UploadAction.java(fileupload.action)
package com.rczp.action;import java.io.File; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport{private static final long serialVersionUID = 1L;private File image; //上傳的文件private String imageFileName; //文件名稱private String imageContentType; //文件類型private String message;//顯示上傳情況public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public File getImage() {return image;}public void setImage(File image) {this.image = image;}public String getImageFileName() {return imageFileName;}public void setImageFileName(String imageFileName) {this.imageFileName = imageFileName;}public String getImageContentType() {return imageContentType;}public void setImageContentType(String imageContentType) {this.imageContentType = imageContentType;}public String execute() throws Exception {System.out.println("上傳圖片的execute方法。。。。。。。。。。。。。。。。");String realpath = ServletActionContext.getServletContext().getRealPath("/images");//D:\apache-tomcat-6.0.18\webapps\struts2_upload\imagesSystem.out.println("realpath: "+realpath);if (image != null) {File savefile = new File(new File(realpath), imageFileName);if (!savefile.getParentFile().exists())savefile.getParentFile().mkdirs();FileUtils.copyFile(image, savefile);message="文件上傳成功";return "result";}else {return "input";}}upload.jsp(在該頁面進行文件上傳)
%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="struts" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><title>upload page</title></head><body><struts:form action="fileupload" enctype="multipart/form-data"><struts:file type="file" name="image"></struts:file><struts:submit label="提交"></struts:submit></struts:form><br/><struts:fielderror /></body></html>uploadResult.jsp(在該頁面顯示上傳結果)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="struts" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><title>uploadResult page</title></head><body>${ message }<br/><br/>文件路徑:<struts:property value="'images/'+imageFileName"/><struts:debug></struts:debug></body> </html>struts.xml中的配置
<struts><!-- 指定允許上傳的文件最大字節數。默認值是2097152(2M) --><constant name="struts.multipart.maxSize" value="10701096"/> <!-- 設置上傳文件的臨時文件夾,默認使用javax.servlet.context.tempdir --><constant name="struts.multipart.saveDir " value="E:/zzmp" /><package name="main" extends="struts-default" ><action name="fileupload" class="com.rczp.action.UploadAction" ><result name="result">/uploadResult.jsp</result><result name="input">/upload.jsp</result><!-- 動態設置savePath的屬性值 --><param name="savePath">/images</param></action></package> </struts>=======分割線==以上為文件上傳==============
文件下載未完待續。。。
總結
以上是生活随笔為你收集整理的【struts2】文件上传下载—struts2实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在JSP页面中使用Ajax主题时的引入
- 下一篇: 文件上传下载—servlet API实现