具体原因是Action中接不到值,但是将上传功能单独拿出来运行成功(不信自己拷贝代码来试),因此排除代码问题,整个项目的其他功能正常运行,在就是控制台没有报错信息,求教高手 问题出在哪里?
由于Strust.xml 中配置太多这里就只贴上传代码
[code="java"]<!-- 文件上传下载 -->
<?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.properties中指定 -->
<!-- 指定Struts2是否处于开发状态 -->
<!-- 指定当struts2配置文件改变后,web框架是否重新加载struts2的配置文件 -->
<!-- 指定需要Struts2处理的请求后缀,该属性的默认值是action -->
<!-- 指定Web应用的默认编码集。对于获取中文请求参数值,应该将该属性值设置为GBK或者GB2312。 -->
<!-- 国际化全局资源文件名i18n -->
<!-- 允许静态方法访问 -->
<!-- 文件上传 -->
<package name="MyRole" extends="struts-default">
<action name="fileAction" class="com.role.action.fileAction">
<interceptor-ref name="fileUpload">
<!-- 为Action中的inputPath属性设值 -->
<param name="inputPath">/upload</param>
<!-- 配置允许上传的文件类型,多个用","分隔 -->
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/pjpeg
</param>
<!-- 配置允许上传的文件大小,单位字节 -->
<param name="maximumSize">1024008*8</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="basicStack"></interceptor-ref>
<result name="input" type="redirect">/files.jsp</result>
</action>
</package>
[/code]
这里是fileAction[code="java"]package com.role.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class fileAction extends ActionSupport {
//路径名
private String inputPath;
private File photo;
private String photoFileName;
private String photoContentType;
private String caption;
public String getInputPath() {
return inputPath;
}
public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}
public File getPhoto() {
return photo;
}
public void setPhoto(File photo) {
this.photo = photo;
}
public String getPhotoFileName() {
return photoFileName;
}
public void setPhotoFileName(String photoFileName) {
this.photoFileName = photoFileName;
}
public String getPhotoContentType() {
return photoContentType;
}
public void setPhotoContentType(String photoContentType) {
this.photoContentType = photoContentType;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
public String execute() throws Exception {
System.out.println("文件描述+++++++"+caption);
if (photo != null) {
System.out.println(photo.length());
System.out.println(photoFileName);
FileInputStream fis = new FileInputStream(photo);
String outfile = ServletActionContext.getServletContext()
.getRealPath("/upload")
+ "/" + photoFileName;
System.out.println(outfile);
FileOutputStream fos = new FileOutputStream(outfile);
byte[] bytes = new byte[2048 * 8];
int len;
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
fis.close();
fos.close();
}
return "input";
}
public String downFile()throws Exception{
return"downfile";
}
/**
* 下载
* @return
* @throws Exception
*/
public InputStream getTargetFile() throws Exception{
return ServletActionContext.getServletContext().
getResourceAsStream("/upload"+photoFileName);
}
}
[/code]
最后是JSP[code="java"]<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
</head>
<body>
<s:form action="fileAction" method="post" theme="simple"
enctype="multipart/form-data">
<!-- 测试文件上传 -->
<s:file key="photo" label="图片文件"></s:file>
<s:textfield key="caption" label="描述"></s:textfield>
<s:submit value="测试按钮"></s:submit>
</s:form>
</body>
[/code]
下面是控制台仅有的信息 但是我感觉没多大关系的说,可是运行啦四次 无解- -[code="java"]文件描述+++++++null
06:21:19,729 INFO FileUploadInterceptor:31 - Removing file photo \tempUploadFile\upload__36291c3_12d7c4e8dd3__8000_00000002.tmp
06:21:19,736 INFO FileUploadInterceptor:31 - Removing file photo \tempUploadFile\upload__36291c3_12d7c4e8dd3__8000_00000002.tmp
06:21:19,736 INFO FileUploadInterceptor:31 - Removing file photo \tempUploadFile\upload__36291c3_12d7c4e8dd3__8000_00000002.tmp
06:21:19,737 INFO FileUploadInterceptor:31 - Removing file photo \tempUploadFile\upload__36291c3_12d7c4e8dd3__8000_00000002.tmp[/code]