用java.util.UUID工具类生产唯一的文件名
上传实例:
struts.xml:
<?xml version="1.0" encoding="GBK" ?> <!--指定struts2配置文件的DTD信息--> <!DOCTYPE struts PUBLIC "-//apache Software Foundation//DTD Struts Configuation 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <!-- struts 是struts2配置文件的根元素--> <struts> <constant name="struts.devMode" value="true"></constant> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <!--允许静态方法的执行--> <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant> <package name="test" namespace="/" extends="struts-default"> <action name="uploadPro" class="cn.edu.hpu.action.UploadAction"> <!-- 动态设置action的属性值 --> <param name="savePath">/upload</param> <!-- 配置Struts 2默认的视图页面 --> <result>/content/succ.jsp</result> </action> <action name="*"> <result>/content/{1}.jsp</result> </action> </package> </struts>
package cn.edu.hpu.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; //封装上传文件域的属性 private File upload; //封装上传文件类型的属性 private String uploadContenttype; //封装上传文件名的属性 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() throws Exception{ return ServletActionContext.getServletContext().getRealPath(savePath); } public void setSavePath(String savePath) { this.savePath = savePath; } public String execute() throws Exception { //以服务器的文件保存地址和原文件名建立上传文件输出流 FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+getUploadFileName()); FileInputStream fis=new FileInputStream(getUpload()); byte [] buffer=new byte[1024]; int len=0; while((len=fis.read(buffer))>0){ fos.write(buffer,0,len); } return SUCCESS; } }
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>upload</title> </head> <body> <s:form action="uploadPro" enctype="multipart/form-data"> <s:textfield name="title" label="文件标题"/><br/> <s:file name="upload" label="选择文件"/><br/> <s:submit value="上传"></s:submit> </s:form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>upload success</title> </head> <body> 上传成功!<br/> 文件标题:<s:property value=" + title"/><br/> 文件为:<img src="<s:property value="'upload/'+uploadFileName"/>"/><br/> </body> </html>
注意:
在struts.xml中配置action,然后注入初始值param,,里面有允许上传的类型。
在action类中,用get、set方法得到允许上传的类型(一般是个字符串数组),之后与上传文件的类型比较,如果有一样的,放行,如果没有,说明上传类型非法,将错误信息放在FieldError里面,类似:addFieldError("upload","上传的类型不正确!");。在Jsp文件里以<s:fielderror/>显示错误提示
转载请注明出处:http://blog.csdn.net/acmman/article/details/47251619