struts2文件上传(单文件部分)

来源:互联网 发布:three.js 中文 编辑:程序博客网 时间:2024/04/29 19:56

整理下struts2文件上传单个文件的部分。


先看看具体效果

输入界面:


结果界面:


前台页面代码

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><!-- 导入struts标签库 --><%@taglib prefix="s" uri="/struts-tags"%><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><form action="uploadAction!upload.action" method="post" enctype="multipart/form-data">文件标题:<input type="text" name="title"><br />选择文件 : <input type="file" name="upload"><br /><input type="submit" value="提交"></form></body></html>

action部分

package com.hcj.action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport {//封装文件标题请求参数的属性private String title;//封装上传文件域的属性(属性名和前台input里name值相同)private File upload;//封装上传文件类型的属性(属性名称为前台name值+ContentType,如此处前台name值为upload,因此此处为uploadContentType)private String uploadContentType;//封装上传文件名的属性(属性名称为前台name值+FileName,如此处前台name值为upload,因此此处为uploadFileName)private String uploadFileName;//配置保存路径(直接在struts.xml文件中配置)private String savePath;public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public File getUpload() {return upload;}public void setUpload(File upload) {this.upload = upload;}public String getUploadContentType() {return uploadContentType;}public void setUploadContentType(String uploadContentType) {this.uploadContentType = uploadContentType;}public String getUploadFileName() {return uploadFileName;}public void setUploadFileName(String uploadFileName) {this.uploadFileName = uploadFileName;}public String getSavePath() {/** * 注意事项 * 1.并不是和其他方法一样,简单的返回savePath * 2.可能有些书上这里返回服务器的路径用的是ServletActionContext.getRequset().getRealPath(savePath), * 但是这个方法已经过期,现用ServletActionContext.getServletContext().getRealPath代替该方法 * 3.如果你第二步方法报The method getServletContext() from the type ServletActionContext refers to the missing type ServletContext错误, * 则是因为少了servlet-api.jar的包,该包在你tomcat目录下的lib文件里可找到,将其导入你WEB-INF下的lib包里即可。 */return ServletActionContext.getServletContext().getRealPath(savePath);}public void setSavePath(String savePath) {this.savePath = savePath;}public String upload() throws Exception {//以服务器的文件保存地址和原文件名建立文件上传输出流File outFile = new File(getSavePath() + "\\" + getUploadFileName());//创建字节输出流FileOutputStream fos = new FileOutputStream(outFile);//创建字节输入流FileInputStream fis = new FileInputStream(getUpload());//设置缓冲区byte[] buffer = new byte[1024];int len=0;//读取上传的文件while((len = fis.read(buffer))>0){//写入目标文件fos.write(buffer, 0, len);}//关流fos.close();fis.close();return SUCCESS;}}



struts.xml配置
<!-- 单文件上传 --><action name="uploadAction" class="com.hcj.action.UploadAction"><!-- 配置文件上传的拦截器 --><interceptor-ref name="fileUpload"><!-- 配置允许上传的文件类型为图片类型,txt,xls,docx --><param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,text/plain,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.wordprocessingml.document</param><!-- 配置允许上传的文件大小 为2M,struts2文件上传默认的大小是2M,如果需要上传超过2M的文件,在struts.xml配置文件中就需要加入这样一句<constant name="struts.multipart.maxSize" value="9000000"/> --><param name="maximumSize">2097152</param></interceptor-ref><!-- 配置系统默认的拦截器 --><interceptor-ref name="defaultStack"></interceptor-ref><!-- 这里的savePath对应了action里的savePath --><param name="savePath">/upload</param><!--定义逻辑视图和物理资源之间的映射 --><result name="success">/suc.jsp</result><result name="error">/error.jsp</result><result name="input">/uploadinput.jsp</result></action>

上传成功后的相应页面

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><base href="<%=basePath%>">      <!-- 导入struts2标签库 --><%@taglib prefix="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>上传成功</title></head><body><label>上传成功</label><!-- 输出上传的表单里的文件标题属性 -->文件标题:<s:property value=" + title"/><br /><!-- 根据上传文件的文件名,在页面上显示上传的图片 -->文件为: <img  src="<s:property value="'upload/' + uploadFileName"/>"/><br/></body></html>

PS: 可通过allowedExtensions来代替上面的allowedTypes,代替后可直接用后缀名来写:

<interceptor-ref name="fileUpload"> <!-- 配置允许上传的文件类型为图片类型,txt,xls,docx --><param name="allowedExtensions">exe,png,jpg,gif,bmp,doc,docx,xls,rar,txt,zip,js,css,msi,pptx</param><!-- 配置允许上传的文件大小 为2M,struts2文件上传默认的大小是2M,如果需要上传超过2M的文件,在struts.xml配置文件中就需要加入这样一句<constant name="struts.multipart.maxSize" value="9000000"/> --><param name="maximumSize">1048576000</param></interceptor-ref>


0 0
原创粉丝点击