在eclipse中创建一个Dynamic Web Project,搭建好struts2开发环境。
第一步:首先将commons-fileupload-x.x.x.jar和commons-io-x.x.x.jar包导入/WEB-INF/lib目录下
第二步:编写jsp页面,代码如下
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>文件上传</title>
</head>
<body>
<center>
<form action="${pageContext.request.contextPath }/test/upload.action" enctype="multipart/form-data" method="post" >
上传文件:<input type="file" name="uploadImage" ><br/>
<input type="submit" value="上传" >
</form>
</center>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>文件上传</title>
</head>
<body>
<center>
<form action="${pageContext.request.contextPath }/test/upload.action" enctype="multipart/form-data" method="post" >
上传文件:<input type="file" name="uploadImage" ><br/>
<input type="submit" value="上传" >
</form>
</center>
</body>
</html>
注意:form表单中enctype的值为"multipart/form-data",输入项input的type值为file,name值和action中的属性值要一样。
第三步:编写action,代码如下
package cn.qust.struts2.action;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public class UploadAction {
private File uploadImage;
private String uploadImageFileName;
private String uploadImageContentType;
public String upload() throws IOException{
String realpath = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload");
System.out.println(realpath);
if(uploadImage!=null){
System.out.println("==");
File saveFile = new File(new File(realpath),uploadImageFileName);
System.out.println("=== " +saveFile.toString());
if(!saveFile.getParentFile().exists()){
saveFile.getParentFile().mkdirs();
}
FileUtils.copyFile(uploadImage, saveFile);
ActionContext.getContext().put("message", "上传成功");
}
return "success";
}
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 getUploadImageContentType() {
return uploadImageContentType;
}
public void setUploadImageContentType(String uploadImageContentType) {
this.uploadImageContentType = uploadImageContentType;
}
}
package cn.qust.struts2.action;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public class UploadAction {
private File uploadImage;
private String uploadImageFileName;
private String uploadImageContentType;
public String upload() throws IOException{
String realpath = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload");
System.out.println(realpath);
if(uploadImage!=null){
System.out.println("==");
File saveFile = new File(new File(realpath),uploadImageFileName);
System.out.println("=== " +saveFile.toString());
if(!saveFile.getParentFile().exists()){
saveFile.getParentFile().mkdirs();
}
FileUtils.copyFile(uploadImage, saveFile);
ActionContext.getContext().put("message", "上传成功");
}
return "success";
}
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 getUploadImageContentType() {
return uploadImageContentType;
}
public void setUploadImageContentType(String uploadImageContentType) {
this.uploadImageContentType = uploadImageContentType;
}
}
步骤很简单,只要struts2入门了。
但我却因为这个花费了3个多小时,反复的检查调试,代码和书上的、和视频上的都一样,可上传文件的时候就提示:无法显示此页面。
后来无意中上传了一个全英文名称的文件,程序突然就好了,上传成功了。我这才发现只要上传文件名中带有中文的文件,就会上述的错误。
经过多次尝试,发现:
不能上传带有中文名称的文件,但可以上传带有中文路径的文件。
特殊文件类型不能上传,至少我上传apk文件就提示:无法显示次页面。