SSH整合后Struts2上传功能失效

具体原因是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]

1个回答

我给你贴我做的
[code="jsp"][/code]
User类中
[code="java"]
private String savePath;
private File userImage;
private String userImageFileName;
private String contentType;[/code]

在Action中添加User属性, 然后只用user中的属性就可以了。
[code="java"]if(user.getUserImage() != null){
String url = ServletActionContext.getRequest().getRealPath(savePath);
String imageFileName = new Date().getTime() + getExtention(user.getUserImageFileName());
File imageFile = new File(url+"/"+imageFileName);
int BUFFER_SIZE = 16 * 1024;
try {
InputStream in = null ;
OutputStream out = null ;
try {

in = new BufferedInputStream( new FileInputStream(user.getUserImage()), BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(imageFile), BUFFER_SIZE);
byte [] buffer = new byte [BUFFER_SIZE];
while (in.read(buffer) > 0 ) {
out.write(buffer);
}
} finally {
if ( null != in) {
in.close();
}
if ( null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}[/code]
[quote]ServletActionContext.getServletContext().

getResourceAsStream("/upload"+photoFileName);[/quote]
xml中的参数没有加“/”,我看你上面的路径在xml中都配置了为什么在取路径的时候还写死呢?
[code="xml"]img[/code]

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!