您的位置:首页 > 编程语言 > Java开发

用Struts2实现文件上传

2014-09-04 14:20 543 查看
  文件的上传和下载是网页开发中经常要遇到的问题,本文将结合MyEclipse+Struts2开发一个文件上传系统。

  本文利用的Java组件是Commons fileUpload(commons-fileupload-1.2.1.jar)

1 文件上传:

  这是实现后的页面效果图:

  


1.1 准备

  打开MyEclipse,建立web project名为DDDemo1,右击MyEclipse→Project facets,选择struts2.x,添加Struts2组件。

  注意:在选择URL pattern时必须选择【/*】,否则将无法读取struts标签,当然如果你不用struts标签就无所谓了╮(╯_╰)╭

1.2 JSP

  首先是输入页面fileUploadInput.jsp,如上图所示,代码为:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>
<title>Upload File</title>
<s:head/>
</head>

<body>
<s:form action = "fileUpload" enctype = "multipart/form-data" method = "post">
<s:file name = "file" label = "File"/>
<s:submit/>
</s:form>
</body>
</html>


  如果经过Action返回SUCCESS,跳转页面为fileUploadSuccess.jsp,同时显示文件名称及扩展名。

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>
<title>Upload File</title>
<s:head/>
</head>

<body>
<h1>File Upload Success</h1>
File Name:<s:property value = "fileFileName"/><br/>
File Content Type:<s:property value = "fileContentType"/>
<s:submit value = "返回" align="left" onclick="javascript:history.go(-1);" />
</body>
</html>


  如果失败,则跳转到fileUploadError.jsp,按键"返回"返回到之前页面

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>
<title>Upload File</title>
<s:head/>
</head>

<body>
<h1>File Upload Error</h1>
<s:submit value = "返回" align="left" onclick="javascript:history.go(-1);" />
</body>
</html>


1.3 文件上传Action页面FileUploadAction

package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {

/**
*
*/
private static final long serialVersionUID = -4311773232805645652L;
private File file;
private String fileContentType;
private String fileFileName;

public String execute() throws Exception {
byte[] buffer = new byte[1024];
InputStream in = new FileInputStream(file);
OutputStream out = new FileOutputStream(new File("f:///Test//"
+ fileFileName));
int length = in.read(buffer);
while (length > 0) {
out.write(buffer);
length = in.read(buffer);
}
in.close();
out.flush();
out.close();

return SUCCESS;
}

public File getFile() {
return file;
}

public void setFile(File file) {
this.file = file;
}

public String getFileFileName() {
return fileFileName;
}

public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}

public String getFileContentType() {
return fileContentType;
}

public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}

}


1.4 struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="qian" extends="struts-default">
<action name="fileUpload" class="action.FileUploadAction">
<result name = "input">fileUploadError.jsp</result>
<result name = "success">fileUploadSuccess.jsp</result>
</action>
</package>
</struts>


1.5 文件类型过滤

这里以图片格式为例,将struts.xml修改为:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="qian" extends="struts-default">
<action name="fileUpload" class="action.FileUploadAction">
<interceptor-ref name="defaultStack">
<!-- 设置文件类型 -->
<param name="fileUpload.allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/jpg
</param>
<!-- 限制文件大小,这里设置10MB -->
<param name="fileUpload.maximumSize">10485760</param>
</interceptor-ref>
<result name = "input">fileUploadError.jsp</result>
<result name = "success">fileUploadSuccess.jsp</result>
</action>
</package>
</struts>


1.6 小结

  对上传系统进行测试,首先输入地址,选择上传的文件:



  成功界面,并在相应磁盘中找到上传的文件:



  失败界面:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: