[b]struts中的文件上传[/b]
在struts2的文件上传中有两点要注意:
[list]
form表单要加enctype="multipart/form-data"属性
action中变量的固定命名规则
[/list]
具体代码如下:
FileUploadAction.java 处理文件上传的action
struts.xml
fileUpload.jsp 文件上传的提交页面:
message.jsp : 显示信息页面
在struts2的文件上传中有两点要注意:
[list]
form表单要加enctype="multipart/form-data"属性
action中变量的固定命名规则
[/list]
具体代码如下:
FileUploadAction.java 处理文件上传的action
package com.sinwee.upload;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 测试目的:struts2中上传文件
* 测试时间:2013年6月23日14:56:11
*
* @author Administrator
*
*/
public class FileUploadAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 331290891668453714L;
private File file;
private String fileFileName; // 规定写法:File的变量名+FileName
/**
* 上传文件
*
* @return
*/
public String upload() {
String realPath = ServletActionContext.getServletContext().getRealPath(
"/upload");
System.out.println(realPath);
if (file != null) {
File saveFile = new File(new File(realPath), fileFileName);
System.out.println(saveFile + " saveFile");
if (!saveFile.getParentFile().exists()) {
saveFile.getParentFile().mkdirs();
}
try {
FileUtils.copyFile(file, saveFile);
ServletActionContext.getRequest().setAttribute("message",
"文件上传成功!");
} catch (IOException e) {
ServletActionContext.getRequest().setAttribute("message",
"文件上传失败!");
e.printStackTrace();
}
}
return "upload";
}
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;
}
}
struts.xml
<?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>
<constant name="strtus.devMode" value="true"></constant>
<package name="fileUpload" namespace="" extends="struts-default">
<action name="fileUpload_*" class="com.sinwee.upload.FileUploadAction" method="{1}">
<result name="upload">
/message.jsp
</result>
</action>
</package>
<!-- Add packages here -->
</struts>
fileUpload.jsp 文件上传的提交页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="${pageContext.request.contextPath }/fileUpload_upload" method="post" enctype="multipart/form-data">
文件:<input type="file" name="file"/><br/>
<input type="submit" value="文件上传"/>
</form>
</body>
</html>
message.jsp : 显示信息页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
文件上传信息:
${message }
</body>
</html>