struts2做上传功能有什么好的方法吗?
不知道struts2中有什么专门的东西

解决方案 »

  1.   

    依然要依靠一些已经使用中的组件。比如
    http://www.duduwolf.com/wiki/2007/334.html如果你熟悉其他的,继续使用吧! struts2 不会自己造一个新的组件出来的!
      

  2.   

    原文:http://hi.baidu.com/miaozilong/blog/item/94dab11ef61ecc1a40341747.htmlpackage action;import java.io.*;
    import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport
    {
        private File upload;
        private String fileName;
        private String uploadContentType;
        
        public String getUploadFileName()
         {
            return fileName;
         }    public void setUploadFileName(String fileName)
         {
            this.fileName = fileName;
         }    public File getUpload()
         {
            return upload;
         }    public void setUpload(File upload)
         {
            this.upload = upload;
         }
        public void setUploadContentType(String contentType)
         {
            this.uploadContentType=contentType;
        
         }
        
        public String getUploadContentType()
         {
            return this.uploadContentType;
         }
        public String execute() throws Exception
         {   
             java.io.InputStream is = new java.io.FileInputStream(upload);
             java.io.OutputStream os = new java.io.FileOutputStream("d:\\upload\\" + fileName);
            byte buffer[] = new byte[8192];
            int count = 0;
            while((count = is.read(buffer)) > 0)
             {
                 os.write(buffer, 0, count);
             }
             os.close();
             is.close();
            return SUCCESS;
         }
    }
      

  3.   

    package action; import java.io.*; 
    import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport 

        private File upload; 
        private String fileName; 
        private String uploadContentType; 
        
        public String getUploadFileName() 
        { 
            return fileName; 
        }     public void setUploadFileName(String fileName) 
        { 
            this.fileName = fileName; 
        }     public File getUpload() 
        { 
            return upload; 
        }     public void setUpload(File upload) 
        { 
            this.upload = upload; 
        } 
        public void setUploadContentType(String contentType) 
        { 
            this.uploadContentType=contentType; 
        
        } 
        
        public String getUploadContentType() 
        { 
            return this.uploadContentType; 
        } 
        public String execute() throws Exception 
        {  
            java.io.InputStream is = new java.io.FileInputStream(upload); 
            java.io.OutputStream os = new java.io.FileOutputStream("d:\\upload\\" + fileName); 
            byte buffer[] = new byte[8192]; 
            int count = 0; 
            while((count = is.read(buffer)) > 0) 
            { 
                os.write(buffer, 0, count); 
            } 
            os.close(); 
            is.close(); 
            return SUCCESS; 
        } 
    }不加包了???
      

  4.   

    在struts2中有一个fileupload拦截器,可以利用yetaodiao的方法进行文件的上传。
      

  5.   

    默认用jarkata的解析器,当然可以选择pell或者cos
    具体:http://blog.sina.com.cn/s/blog_4ef8aa5601009k6i.html
      

  6.   

    http://www.blogjava.net/max/archive/2007/03/21/105124.html
      

  7.   

    struts2中可以这么做上传
    jsp中
    <s:form id="form1" name="form1"  method="post" enctype="multipart/form-data" action="/doUpload.action">
    <table>
    <tr>
    <td>
    <s:file name="file" label="附件"/><s:textfield name="caption" label="名称" theme="simple"></s:textfield>
    </td>
    <td><s:submit value="上传" ></s:submit></td>
    </tr>
    </table>
    </s:form>domain中
    public class MyFile implements java.io.Serializable {
    private byte[] uploadFile;
    private String FileName;
    public String getFileName() {
    return FileName;
    }
    public void setFileName(String fileName) {
    FileName = fileName;
    }
    public byte[] getUploadFile() {
    return uploadFile;
    }
    public void setUploadFile(byte[] uploadFile) {
    this.uploadFile = uploadFile;
    }}
    action中
    public class UploadAction extends ActionSupport {
    private File file;
        private String fileContentType;
        private String fileFileName;
        private String caption;    private UploadService myUploadService; @Override
    public String execute() throws Exception {
    System.out.println("开始上传附件");
    MyFile myFile = new MyFile();
    FileInputStream is = new FileInputStream(this.file);
    byte[] b = new byte[is.available()];
    is.read(b);
    myFile.setUploadFile(b);
    this.myUploadService.saveOrUpdate(myFile);
    return SUCCESS;
    } public File getFile() {
    return file;
    }
    public void setFile(File file) {
    this.file = file;
    } public String getFileContentType() {
    return fileContentType;
    } public void setFileContentType(String fileContentType) {
    this.fileContentType = fileContentType;
    }
    public String getFileFileName() {
    return fileFileName;
    } public void setFileFileName(String fileFileName) {
    this.fileFileName = fileFileName;
    }
    public String getCaption() {
    return caption;
    } public void setCaption(String caption) {
    this.caption = caption;
    }
    ......}
      

  8.   

    用个上传组传,提交后返回文件名,用struts保存文件名
      

  9.   

    struts2默认使用的是jakarta的common_fileupload的文件上传框架。 你要用strtus2 的文件上传功能。需要在web应用中加两个jar文件。 commons_io_1.3.jar 和 commons_fileupload-1.2.jar。你可以参考《struts 2权威指南》中有详细的说明。