Java三大框架之struts的上传文件
2016-07-23 19:28
363 查看
在没有struts之前,想要用Java上传文件还得写很多复杂的代码,利用servlet3.0的新特性,或者在servlet3.0之前比如借助其他的插件,比如commons-fileupload组件,或者其他的。并且代码写得十分复杂。利用struts框架进行上传文件核心代码只需要几行。但是框架用起来感觉有点复杂的就是搭建这个环境了。
首先应该包含核心jar包。这些文件可以从网上下载。我用到的核心jar包如下:
第二步:struts是基于拦截器实现的。因此需要在web.xml里面配置一个拦截器,把控制权交给struts来处理。
<filter>
<filter-name>Struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>第三步:在struts.xml文件中写action的配置
<?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="fileAction" extends="struts-default" namespace="/">
<action name="MyfileUpload" class="com.levi.action.FileUpload">
<result name="success">/fileUpload_Success.jsp</result>
<result name="error">/error.jsp</result>
<result name="input">/fileUpload.jsp</result>
</action>
</package>
</struts>第四步前端上传页面代码为:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@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>Insert title here</title>
</head>
<body>
<form action="MyfileUpload" method="post" enctype="multipart/form-data">
<input type="file" name="fileUpload"/><br>
<button type="submit">上传</button>
</form>
<br>
错误信息如下:<s:fielderror></s:fielderror>
</body>
</html>前端上传成功页面如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@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>Insert title here</title>
</head>
<body>
文件上传成功<br>
文件名:<s:property value="#session.fileUploadFileName"/><br>
文件类型为:<s:property value="#session.fileUploadContentType"/><br>
</body>
</html>最后编写处理Action的类
package com.levi.action;
import java.io.File;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUpload extends ActionSupport
{
/**
*
*/
private static final long serialVersionUID = 1L;
private File fileUpload;
private String fileUploadFileName;
public File getFileUpload()
{
return fileUpload;
}
public void setFileUpload(File fileUpload)
{
this.fileUpload = fileUpload;
}
public String getFileUploadFileName()
{
return fileUploadFileName;
}
public void setFileUploadFileName(String fileUploadFileName)
{
this.fileUploadFileName = fileUploadFileName;
}
public String getFileUploadContentType()
{
return fileUploadContentType;
}
public void setFileUploadContentType(String fileUploadContentType)
{
this.fileUploadContentType = fileUploadContentType;
}
private String fileUploadContentType;
@Override
public String execute() throws Exception
{
ActionContext actionContext=ActionContext.getContext();
Map<String, Object> session=actionContext.getSession();
session.put("fileUploadFileName", fileUploadFileName);
session.put("fileUploadContentType", fileUploadContentType);
System.out.println("文件名为:"+fileUploadFileName);
System.out.println("文件的类型为:"+fileUploadContentType);
String filePath="d:/upload/"+fileUploadFileName;
File saveFile=new File(filePath);
FileUtils.copyFile(fileUpload, saveFile);
return SUCCESS;
}
}
然后就可以实现上传了。这个绝对简单很多的,只是前面配置有点麻烦。这个只实现了一个单文件的上传,后面可以增加一些配置,比如设定最大文件大小,上传类型等等,下一篇再详细实现
首先应该包含核心jar包。这些文件可以从网上下载。我用到的核心jar包如下:
第二步:struts是基于拦截器实现的。因此需要在web.xml里面配置一个拦截器,把控制权交给struts来处理。
<filter>
<filter-name>Struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>第三步:在struts.xml文件中写action的配置
<?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="fileAction" extends="struts-default" namespace="/">
<action name="MyfileUpload" class="com.levi.action.FileUpload">
<result name="success">/fileUpload_Success.jsp</result>
<result name="error">/error.jsp</result>
<result name="input">/fileUpload.jsp</result>
</action>
</package>
</struts>第四步前端上传页面代码为:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@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>Insert title here</title>
</head>
<body>
<form action="MyfileUpload" method="post" enctype="multipart/form-data">
<input type="file" name="fileUpload"/><br>
<button type="submit">上传</button>
</form>
<br>
错误信息如下:<s:fielderror></s:fielderror>
</body>
</html>前端上传成功页面如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@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>Insert title here</title>
</head>
<body>
文件上传成功<br>
文件名:<s:property value="#session.fileUploadFileName"/><br>
文件类型为:<s:property value="#session.fileUploadContentType"/><br>
</body>
</html>最后编写处理Action的类
package com.levi.action;
import java.io.File;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUpload extends ActionSupport
{
/**
*
*/
private static final long serialVersionUID = 1L;
private File fileUpload;
private String fileUploadFileName;
public File getFileUpload()
{
return fileUpload;
}
public void setFileUpload(File fileUpload)
{
this.fileUpload = fileUpload;
}
public String getFileUploadFileName()
{
return fileUploadFileName;
}
public void setFileUploadFileName(String fileUploadFileName)
{
this.fileUploadFileName = fileUploadFileName;
}
public String getFileUploadContentType()
{
return fileUploadContentType;
}
public void setFileUploadContentType(String fileUploadContentType)
{
this.fileUploadContentType = fileUploadContentType;
}
private String fileUploadContentType;
@Override
public String execute() throws Exception
{
ActionContext actionContext=ActionContext.getContext();
Map<String, Object> session=actionContext.getSession();
session.put("fileUploadFileName", fileUploadFileName);
session.put("fileUploadContentType", fileUploadContentType);
System.out.println("文件名为:"+fileUploadFileName);
System.out.println("文件的类型为:"+fileUploadContentType);
String filePath="d:/upload/"+fileUploadFileName;
File saveFile=new File(filePath);
FileUtils.copyFile(fileUpload, saveFile);
return SUCCESS;
}
}
然后就可以实现上传了。这个绝对简单很多的,只是前面配置有点麻烦。这个只实现了一个单文件的上传,后面可以增加一些配置,比如设定最大文件大小,上传类型等等,下一篇再详细实现
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树