struts2初学之form上传

以前struts1使用的FormFile我不是很喜欢,这下好了,struts2现在直接拿到File了,OK,上代码!

需要上传的话要用到2个jar包:commons-io-x.jar和commons-fileupload-x.jar导入到lib后即可。

PS:我的工程名字叫TestStruts

1.写action:(BaseAction.java见第一章节)

FormFileStruts2.java:

package com.xuyi.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FormFileStruts2 extends BaseAction {
	
	public static final String ROOT = "upload\\";
	
	private String username;
	
	//可以拿到file的流,使用tmp文件
	private File image;
	
	//固定格式取file的真实文件名
	private String imageFileName;
	
	//固定格式
	private String imageContentType;
	
	private String msg;
	
	@Override
	public String execute() throws Exception {
		//配置src下面的struts.properties中加入struts.multipart.saveDir = /tmp
		//否则在上传文件时会出现Unable to find 'struts.multipart.saveDir' property setting的错误
		
		String rootPath = getSession().getServletContext().getRealPath("/");
		rootPath += ROOT;
		mkDirectory(rootPath);
		System.out.println(rootPath);
		try {
			upload(imageFileName, rootPath, image);
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		
		msg = imageFileName;
		return SUCCESS;
	}
	
	/**
	 * 根据路径创建一系列的目录
	 * 
	 * @param path
	 */
	boolean mkDirectory(String path) {
		File file = null;
		try {
			file = new File(path);
			if (!file.exists()) {
				return file.mkdirs();
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		} finally {
			file = null;
		}
		return false;
	}
	
	/**
	 * 上传文件
	 * 
	 * @param savePath
	 *            文件的保存路径
	 * @param uploadFile
	 *            被上传的文件
	 * @return uploadFileName
	 */
	String upload(String uploadFileName, String savePath, File uploadFile) {
		FileOutputStream fos = null;
		FileInputStream fis = null;
		try {
			fos = new FileOutputStream(savePath + uploadFileName);
			fis = new FileInputStream(uploadFile);
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				fos.close();
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return uploadFileName;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}
	
	public String getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getMsg() {
		return msg;
	}

	
}
 

src下面新建一个struts.properties文件:

struts.multipart.saveDir = /tmp

 

2.配置struts.xml

<!DOCTYPE struts PUBLIC 
	"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
	"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<!--package: name可以随便指定  namespace指定页面的目录(/为根目录) extends指定为 struts-default.xml文件(此文件包含默认的设置和定义) -->
	<package name="struts" namespace="/" extends="struts-default">
		<!--action:name是访问action的名称 class是action的类 method是访问的action里的方法等同于struts1的method result是返回url-->
			
		<!-- struts2 file_upload begin -->
		<action name="formFileStruts2" class="com.xuyi.action.FormFileStruts2">
			<result name="success">/pages/struts2_form_file_result.jsp</result>
		</action>		
		<!-- struts2 file_upload end -->
	</package>
</struts>
 

3.在WebRoot下建立pages文件夹中创建jsp

struts2_form_file_submit.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Struts2-form上传示例-提交页</title>
</head>
<body>
	<form action="../formFileStruts2.action" method="post" enctype="multipart/form-data">
		用户名:<input type="text" name="username"><br/><br/>
		图片:<input type="file" name="image"><br/><br/>
		<input type="submit" value="提交">
	</form>
</body>
</html>

 struts2_form_file_result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Struts2-form上传示例-结果页</title>
</head>
<body>
	${username}上传图片${msg}成功!
</body>
</html>

 4.访问:http://localhost:8080/TestStruts/pages/struts2_form_file_submit.jsp即可操作

你可能感兴趣的:(apache,html,jsp,xml,struts)