关于Struts2文件上传的问题,首先要准备好一下几个包
新建一个项目,整体架构如下:
接着就通过一下实例来实现吧。
1、UploadAction.java
- package com.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; //封装上传文件名的属性
- private String savePath; //直接在Struts.xml文件中配置的属性
- @Override
- public String execute() throws Exception{
- FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName());
- /*FileOutputStream fos = new FileOutputStream(ServletActionContext.getServletContext()
- .getRealPath(getSavePath())+ "\\" + getUploadFileName());*/
- //FileOutputStream fos = new FileOutputStream("e:\\upload" + "\\" + 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;
- }
- 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;
- }
- /**
- * 返回上传文件的保存
- * @return
- */
- public String getSavePath() {
- return ServletActionContext.getServletContext().getRealPath(savePath);
- }
- public void setSavePath(String savePath) {
- this.savePath = savePath;
- }
- }
2、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>
- <constant name="struts.custom.i18n.resources" value="mess" />
- <constant name="struts.i18n.encoding" value="UTF-8" />
- <constant name="struts.devMode" value="true"></constant>
- <package name="lee" extends="json-default">
- <action name="uploadAction" class="com.action.UploadAction">
- <param name="savePath">/upload</param>
- <result>success.jsp</result>
- </action>
- </package>
- </struts>
3、index.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>文件上传</title>
- </head>
- <body>
- <form action="uploadAction.action" method="post" enctype="multipart/form-data">
- 文件名称 : <input type="text" name="title"><br>
- 文 件 :<input type="file" name="upload"><br>
- <input type="submit" value=" 上传 ">
- </form>
- </body>
- </html>
4、Success.jsp
- <%@ 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>上传成功</title>
- </head>
- <body>
- 文件<b>${uploadFileName}</b>上传成功! <br>
- </body>
- </html>
然后部署后运行效果图如下:
点击上传文件后: