十一、struts2文件上传步骤

来源:互联网 发布:数据对比分析模版 编辑:程序博客网 时间:2024/06/15 08:18
1:

•下载Common-FileUplaod框架地址:

http://jakarta.apache.org/commons/fileupload/

–下载commons-fileupload-1.2-bin.zip文件

–解压后得到commons-fileupload-1.2.jar

– 

http://jakarta.apache.org/commons/io/

–下载commons-io-1.3.2-bin.zip文件

–解压后得到commons-io-1.3.2.jar

(网络不好可以从百度云盘上获取:https://pan.baidu.com/s/1o7IgiBC)

二:建立fileup.jsp文件文件内容如下

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s" %><!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><form action="fileup" method="post" enctype="multipart/form-data"> <input type="file" name="file" />    <input type="file" name="file" />    <input type="file" name="file" />    <input type="file" name="file" />    <input type="file" name="file" />    <br/><input type="submit" value="确定"/></form> ${message }</body></html>
配置struts.xml文件

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><package name="base" namespace="" extends="struts-default"><global-results><result name="err">/err.jsp</result><result name="ajax">/ajax.jsp</result><result name="noLogin">/login.jsp</result></global-results></package><!-- 配置常量 --><constant name="struts.i18n.encoding" value="utf-8"/><!-- 配置xml更新后xml重新加载而不用来回启动tomcat --><constant name="struts.configuration.xml.reload" value="true"/><!-- 打印出更详细错误信息配置一般有错误时会进行配置--><!-- <constant name="struts.devMode=true" value="true"/> --><constant name="struts.multipart.maxSize" value="20971520"></constant><!-- 配置常量end --><!-- 导入其他STRUTS配置文件 --><include file="struts-user.xml"></include><include file="struts-demo.xml"></include></struts>

配置stuts-demo.xml文件

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><package name="demo" namespace="" extends="base"><action name="fileup" class="action.FileupAction" ><result name="ok">/fileup.jsp</result></action></package></struts>

建立FileupAction.java文件(普通class)代码如下

package action;import java.io.File;import java.io.IOException;import java.util.Date;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;public class FileupAction {private File[] file;// 上传的文件private String[] fileContentType;// 上传的文件类型private String[] fileFileName;// 上传的文件名称private String message;//提示消息public File[] getFile() {return file;}public void setFile(File[] file) {this.file = file;}public String[] getFileContentType() {return fileContentType;}public void setFileContentType(String[] fileContentType) {this.fileContentType = fileContentType;}public String[] getFileFileName() {return fileFileName;}public void setFileFileName(String[] fileFileName) {this.fileFileName = fileFileName;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public String execute(){//获取文件上传服务器上的文件夹的物理路径String path=ServletActionContext.getServletContext().getRealPath("/upload");File upPath=new File(path);//判断文件夹是否存在 ,如果不存在,则创建if(!upPath.exists()){upPath.mkdirs();//创建文件夹}String str="";for(int i=0;i<file.length;i++){if(file[i]==null)continue;//如果获取文件失败跳过//获取文件名后缀String ext=fileFileName[i].substring(fileFileName[i].lastIndexOf("."));//设置新的文件名称 String newFileName=new Date().getTime()+ext;//将文件保存到服务器try {FileUtils.copyFile(file[i], new File(path, newFileName));str+="<br>文件上传成功,文件名称:"+newFileName+",文件类型:"+fileContentType[i];} catch (IOException e) {e.printStackTrace();str+="<br>文件上传失败:"+e.getMessage();}}this.message=str;return "ok";}}

项目结构如下

完成

0 0