粘上自己的代码:
ShowWords.java
package action;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
public class ShowWords{
private File uploadImage;
private String uploadImageContentType;
private String uploadImageFileName;
public File getUploadImage() {
return uploadImage;
}
public void setUploadImage(File uploadImage) {
this.uploadImage = uploadImage;
}
public String getUploadImageContentType() {
return uploadImageContentType;
}
public void setUploadImageContentType(String uploadImageContentType) {
this.uploadImageContentType = uploadImageContentType;
}
public String getUploadImageFileName() {
return uploadImageFileName;
}
public void setUploadImageFileName(String uploadImageFileName) {
this.uploadImageFileName = uploadImageFileName;
}
public String execute() {
String realpath = ServletActionContext.getServletContext().getRealPath("/image");
File f=new File(realpath);
if(!f.exists())
{ f.mkdirs(); }
try {
FileUtils.copyFile(uploadImage,new File(f,uploadImageFileName));
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
}
inf.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 'inf.jsp' starting page</title>
</head>
<body>
<form action="showWords.action" method="post" enctype="multipart/form-data">
文件: <input type="file" name="uploadImage">
<input type="submit" name="提交">
</form>
</body>
</html>
要注意:form表单name属性要与ShowWords中的File uploadImage名称相同。
多文件上传,就是要把 uploadImage uploadImageFileName改为数组,原来form表单上几个同时提交的数据的名称要设为一致的。
代码:
ShowWords.java
package action;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
public class ShowWords{
private File[] uploadImage;
private String[] uploadImageFileName;
public File[] getUploadImage() {
return uploadImage;
}
public void setUploadImage(File[] uploadImage) {
this.uploadImage = uploadImage;
}
public String[] getUploadImageFileName() {
return uploadImageFileName;
}
public void setUploadImageFileName(String[] uploadImageFileName) {
this.uploadImageFileName = uploadImageFileName;
}
public String execute() {
String realpath = ServletActionContext.getServletContext().getRealPath("/image");
File f=new File(realpath);
if(!f.exists())
{ f.mkdirs(); }
try {
for(int i=0;i<uploadImage.length;++i)
{
FileUtils.copyFile(uploadImage[i],new File(f,uploadImageFileName[i]));
}
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
}
inf.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 'inf.jsp' starting page</title>
</head>
<body>
<form action="showWords.action" method="post" enctype="multipart/form-data">
文件: <input type="file" name="uploadImage"><br/>
文件: <input type="file" name="uploadImage"><br/>
<input type="submit" name="提交">
</form>
</body>
</html>