您的位置:首页 > 编程语言 > Java开发

struts2之文件上传

2015-08-25 16:36 411 查看
一、单文件上传

实例:

表单应该注意三个点 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>


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: