Struts上传文件

时间:2023-03-08 17:00:12

Struts上传文件分为两个步骤:

1). 首先将客户端上传的文件保存到Struts.multipart.saveDir键所指定的目录中,如果该键所对应的目录不存在,那么就保存到javax.servlet.context.tempdir环境变量所指定的目录中。

2). Action中所定义的File类型的成员变量实际上是指向的是临时目录中的临时文件,然后在服务器端通过IO的方式将临时文件写入到指定的服务器端目录中

环境搭建:

首先看一下目录结构:

Struts上传文件

在对应的文件夹下创建以上图片中的文件

1、在com.action中新建文件上传的action,

 package com.action;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial")
public class UploadAction extends ActionSupport { private String username; //用户名字
private File file; //上传的文件
private String fileFileName; //获取文件名字,命名格式固定为XXXFileName
private String fileContentType; //获取文件的内容类型格式固定为XXXContentType
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 File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String execute() throws Exception {
//获取目标文件的路径
String root = ServletActionContext.getRequest().getRealPath("/upload");
//创建文件输入流
InputStream input = new FileInputStream(file); System.out.println("fileFileName: " + fileFileName);
//在root目录下创建一个名字为fileFileName的文件
File destFile = new File(root,fileFileName);
//上传的文件输出到destFile文件中
OutputStream out = new FileOutputStream(destFile); byte[] buffer = new byte[1024]; int length = 0; while((length = input.read(buffer)) != -1){
out.write(buffer,0,length);
} input.close();
out.close(); return SUCCESS;
}
}

2、在struts.properties属性文件中指定缓冲临时文件的临时目录的路径

struts.multipart.saveDir =E\:/programTempFile

需要在E盘的根目录下创建一个programTempFile文件

3、fileUpload.jsp页面代码

 <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>My JSP 'fileUpload.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<!-- 文件上传表单的提交方式必须是“post” 编码类型必须为:enctype="multipart/form-data" -->
<form action="fileUpload.action" method="post" enctype="multipart/form-data"> username: <input type="text" name="username" /><br>
file: <input type="file" name="file"><br>
<input type="submit" value="上传文件"> </form> </body>
</html>

fileUploadResult.jsp代码

 <%@ page language="java" import="java.util.*,java.io.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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>My JSP 'fileUploadResult.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body> username: <s:property value="username"/><br>
name: <s:property value="fileFileName" /><br>
fileContentType: <s:property value="fileContentType"/><br> </body>
</html>

3、struts.xml中配置信息

 <struts>
<package name="default" extends="struts-default">
<!-- 文件上传下载action -->
<action name="fileUpload" class="com.action.UploadAction"> <result name="success">/fileUploadResult.jsp</result> </action>
</package> </struts>

文件上传界面:

Struts上传文件

上传信息显示

Struts上传文件

服务器端目标文件

Struts上传文件