Struts2文件上传
Posted canmengqian
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2文件上传相关的知识,希望对你有一定的参考价值。
Struts2提供了文件上传的框架,Struts2默认的文件上传框架是cos,大家也可以根据自己的需要修改Struts2中的文件上传框架,对于文件上传来说,无论使用Struts2中的哪一种框架,上传方式都是比较简单的。下面用一个案例介绍Struts2的文件上传。
该案例比较简单,允许用户进行文件上传,上传成功提示文件上传的主题,上传失败则提示上传失败的提示,当然为了防止恶意上传,我专门做了一个简单的文件过滤,限制文件的格式
首先看页面的操作
上传成功的情况
上传失败的情况
接下来分析源码
uploadFile.jsp页面
<%@ page language="java" 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> <s:form action="upload" enctype="multipart/form-data"> <s:textfield name="title">文件标题</s:textfield> <s:file name="upload" label="选择文件"></s:file> <s:submit value="上传"></s:submit> </s:form> </body> </html>
上传文件对应的Action
package test.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 fileUploadAction extends ActionSupport { private String title; private File upload; private String uploadContentType; private String uploadFileName; private String savePath; // 设置文件格式 private String allowType; public String getAllowType() { return allowType; } public void setAllowType(String allowType) { this.allowType = allowType; } 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() { return ServletActionContext.getServletContext().getRealPath(savePath); } public void setSavePath(String savePath) { this.savePath = savePath; } @Override public String execute() throws Exception { // 获取文件内容 FileInputStream fin = new FileInputStream(getUpload()); // 定义一个byte数组 byte[] buffer = new byte[1024]; // 建立文件输出流,建立文件位置 FileOutputStream fout = new FileOutputStream(getSavePath() + "\\\\" + getUploadFileName()); // 每读完1M的量就往文件里写 int len = 0; while ((len = fin.read(buffer)) > 0) { fout.write(buffer, 0, len); } System.out.println("fileUploadAction [title=" + title + ", upload=" + upload + ", uploadContentType=" + uploadContentType + ", uploadFileName=" + uploadFileName + ", savePath=" + savePath + "]"); return SUCCESS; } public String filterFileType(String[] types) { // 获取文件的格式 String fileType = this.getUploadContentType(); System.out.println(fileType); if (fileType != null) { for (String Type : types) { if (Type.equals(fileType)) { return null; } else return ERROR; } } return ERROR; } @Override public void validate() { //接收验证结果 String Result=this.filterFileType(getAllowType().split(",")); if(Result!=null) { addFieldError("uploadError", "该文件格式 不正确"); } } }
Struts.xml代码
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <package name="upload" extends="struts-default"> <action name="upload" class="test.Action.fileUploadAction"> <param name="savePath">/WEB-INF/uploadFiles</param> <param name="allowType">image/x-png,</param> <result>/success.jsp</result> <result name="input">/uploadError.jsp</result> </action> </package> </struts>
分别对应的成功和失败两个jsp 视图
<%@ page language="java" 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> 上传成功 文件标题:<s:property value="title" /><br> 文件为:<s:property value="upload"/> </body> </html>
<%@ page language="java" 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>
<s:fielderror name="uploadError"></s:fielderror>
</body>
</html>
以上是关于Struts2文件上传的主要内容,如果未能解决你的问题,请参考以下文章