路峰
2011-05-04 09:55
浏览 267
已采纳

struts2的文件上传问题

为什么我按照网上那些例子做的struts2文件上传,在action中却得不到表单中的啊 :cry:

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

7条回答 默认 最新

  • 敬敬11 2011-05-09 13:51
    已采纳

    有点怀疑你的action走没走,连个提交按钮都没有

    打赏 评论
  • 敬敬11 2011-05-04 11:26

    得不到表单中的什么?

    打赏 评论
  • 敬敬11 2011-05-04 14:26

    enctype="multipart/form-data" form表单中有这个属性吗

    打赏 评论
  • katy1206 2011-05-04 14:46

    直接上代码阿,不知道你参考的哪里的上传方法。

    打赏 评论
  • Mark写代码 2011-05-04 15:02

    你也没贴代码,我就不知道你错在什么地方了,给一个以前学习Struts2时上传文件的代码吧!!!
    上传文件的页面:
    [code="java"]
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">



    Struts2 File Upload Page



    文件标题:

    选择文件:




    [/code]
    Action:
    [code="java"]
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import com.opensymphony.xwork2.ActionSupport;
    public class UploadAction extends ActionSupport {
    private String title;
    private File upload;
    private String uploadContentType;
    private String uploadFileName;
    private String savePath;
    public String getTitle() {
    return title;
    }
    public void setTitle(String title) {
    this.title = title;
    }
    public File getUpload() {
    return upload;
    }
    public void setUpload(File upload) {
    this.upload = upload;
    }
    public String getUploadContentType() {
    return uploadContentType;
    }
    public void setUploadContentType(String uploadContentType) {
    this.uploadContentType = uploadContentType;
    }
    public String getUploadFileName() {
    return uploadFileName;
    }
    public void setUploadFileName(String uploadFileName) {
    this.uploadFileName = uploadFileName;
    }
    public String getSavePath() {
    return savePath;
    }
    public void setSavePath(String savePath) {
    this.savePath = savePath;
    }
    public String execute() throws Exception {
    System.out.println(getSavePath());
    FileOutputStream fos = new FileOutputStream(getSavePath()+"\"+ getUploadFileName());
    FileInputStream fis = new FileInputStream(getUpload());
    byte[] buffer = new byte[1024];
    int len = 0;
    while ((len = fis.read(buffer)) > 0) {
    fos.write(buffer, 0, len);
    }
    return SUCCESS;
    }
    }
    [/code]
    配置Action:[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">




    D:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\Chapter4Example2\UploadFile
    /succ.jsp



    [/code]
    web.xml:
    [code="java"]
    <?xml version="1.0" encoding="UTF-8"?>
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    struts
    org.apache.struts2.dispatcher.FilterDispatcher


    struts
    /*


    [/code]
    需要添加一个返回成功后的页面。

    打赏 评论
  • iteye_4734 2011-05-04 16:53


    中的action="upload"是提交路径把,你的xml配置了吗?
    你是struts1 应该写upload.do

    打赏 评论
  • Mark写代码 2011-05-05 08:46

    配置中的action="upload"是否该写成action="upload.action"

    打赏 评论

相关推荐 更多相似问题