码迷,mamicode.com
首页 > Web开发 > 详细

struts2之文件上传

时间:2015-08-25 19:16:56      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

一、单文件上传

实例:

表单应该注意三个点   form中的method="post"、enctype="multipart/form-data"、input中的type="file"

fileForm.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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>File Operation</title>
</head>
<body>
	<form action="file/fileOperation_upload" method="post"
		enctype="multipart/form-data">
		<label for="myFile">Upload your file</label><br />
		 <input type="file" name="myFile"> 
		<input type="submit" value="上传">
	</form>
</body>
</html>

struts.xml中配置文件

<package name="file" namespace="/file" extends="struts-default">
     	<action name="fileOperation_*" class="com.lz.web.action.FileOperation" method="{1}" >
     		<result name="upload">/WEB-INF/pages/file.jsp</result>
     		<result name="error">/WEB-INF/pages/error.jsp</result>
     	</action>
     </package>

FileOperationAction.java

package com.lz.web.action;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class FileOperation extends ActionSupport{
	
	private File myFile;
	private String myFileContentType;
	private String myFileFileName;
	private String destPath;

	public File getMyFile() {
		return myFile;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	public String getMyFileContentType() {
		return myFileContentType;
	}

	public void setMyFileContentType(String myFileContentType) {
		this.myFileContentType = myFileContentType;
	}

	public String getMyFileFileName() {
		return myFileFileName;
	}

	public void setMyFileFileName(String myFileFileName) {
		this.myFileFileName = myFileFileName;
	}

	public String upload(){
		
		destPath= ServletActionContext.getServletContext().getRealPath("/files");//文件保存的路径
		System.out.println("realpath: "+destPath);
		System.out.println("Src filename: "+myFile);
		System.out.println("Dest filename: "+myFileFileName);
		try {
			File destFile = new File(destPath, myFileFileName);
			FileUtils.copyFile(myFile, destFile);//拷贝文件
		} catch (IOException e) {
			e.printStackTrace();
			return "error";
		}
		
		return "upload";
	}
	public String prefile(){
		
		return "prefile";
	}
}

file.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s"  uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
your have successfully uploaded <s:property value="myFileFileName"/>
</body>
</html>

二、多文件上传

fileForm2.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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>File Operation</title>
</head>
<body>
	<form action="file/multiFileUploadAction_upload" method="post"
		enctype="multipart/form-data">
	   文件1:<input type="file" name="image"><br/>
            文件2:<input type="file" name="image"><br/>
            文件3:<input type="file" name="image"><br/>
		<input type="submit" value="上传">
	</form>
</body>
</html>

struts.xml中配置

<package name="mfile" namespace="/file" extends="struts-default">
     	<action name="multiFileUploadAction_*" class="com.lz.web.action.MultiFileUploadAction" method="{1}">
     	      <result name="load">/WEB-INF/pages/fileForm2.jsp</result>
     	      <result name="upload2">/WEB-INF/pages/file2.jsp</result>
     	</action>
     </package>

MultiFileUploadAction.java

package com.lz.web.action;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class MultiFileUploadAction extends ActionSupport{
	
	private File[] image;
	private String[] imageFileName;
	private String[] imageContentType;
	
	public File[] getImage() {
		return image;
	}
	public void setImage(File[] image) {
		this.image = image;
	}
	public String[] getImageFileName() {
		return imageFileName;
	}
	public void setImageFileName(String[] imageFileName) {
		this.imageFileName = imageFileName;
	}
	public String[] getImageContentType() {
		return imageContentType;
	}
	public void setImageContentType(String[] imageContentType) {
		this.imageContentType = imageContentType;
	}
	public String load(){
		return "load";
	}
	public String upload(){
		
		String realPath = ServletActionContext.getServletContext().getRealPath("/files");//指定文件保存的路径
		System.out.println("realpath:  "+realPath);
		try {
			if(image!=null){
				File savedir = new File(realPath);
				if(!savedir.getParentFile().exists())//判断文件目录是否存在
					savedir.getParentFile().mkdirs();
				for(int i=0; i<image.length; i++){
					File saveFile = new File(realPath, imageFileName[i]);
					FileUtils.copyFile(image[i], saveFile);//文件复制
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "upload2";
	}

}

file2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
   上传成功<br/>
   <s:iterator value="imageFileName" var="ifn">
      <s:property value="ifn"/>
   </s:iterator>
</body>
</html>

  

 

struts2之文件上传

标签:

原文地址:http://www.cnblogs.com/lzeffort/p/4757872.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!