先贴下代码:
uploadFile.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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>Upload File Page</title>
</head>
<body>
<form action="toUpload" method="post" enctype="multipart/form-data">
<label for="image">Test Struts Upload File</label>
<input type="file" name="image"/><br/>
<input type="submit" value="上传"/>
</form>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.multipart.saveDir" value="c:/temp"/>
<package name="toUpLoad" extends="struts-default">
<action name="toUpload" class="com.acbili.actions.UploadFileAction" >
<interceptor-ref name="basicStack"/>
<interceptor-ref name="fileUpload"/>
<result name="success">/WEB-INF/jsp/success.jsp</result>
<result name="error">/WEB-INF/jsp/error.jsp</result>
</action>
</package>
</struts>
UploadFileAction.java
package com.acbili.actions;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadFileAction extends ActionSupport{
private File image; //上传的文件
private String imageFileName; //文件名称
private String imageContentType; //文件类型
public String execute() throws Exception {
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
System.out.println("realpath: "+realpath);
if (image != null) {
System.out.println("aasssss");
System.out.println("文件名:"+imageFileName);
File savefile = new File(new File(realpath), imageFileName);
if (!savefile.getParentFile().exists())
savefile.getParentFile().mkdirs();
FileUtils.copyFile(image, savefile);
}
return "success";
}
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;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
------解决思路----------------------
换个名字试试,或者手动取下值。
------解决思路----------------------
String realpath = ServletActionContext.getServletContext().getRealPath("/images"); 这句话能读到么?
------解决思路----------------------
拦截器先注释掉,或者再换个浏览器看看。
------解决思路----------------------
<!—文件上传栈-->
<interceptor-stack name="fileUploadStack">
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="basicStack"/>
</interceptor-stack>
试试这个
------解决思路----------------------
直接调要getImage()判断,或将set和get方法置前,image和jar包有冲突改名字。