etgundam1 2011-01-13 06:07
浏览 305
已采纳

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条回答 默认 最新

  • 勤劳的小猿 2011-01-13 09:03
    关注

    我给你贴我做的
    [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]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误