我不信,struts真的这么差!!

解决方案 »

  1.   

    呵呵,什么叫差??
    为什么用这个,struts中不是有个上传的类
      

  2.   

    为什么用这个,struts中不是有个上传的类
    --------------------------------------------因为struts中只是提供的上传,没有下载,虽然可以自己写,但是人家jspsmartupload已经写好了,直接用也很好啊我以为action是基于servlet封装,在servlet中可以,在action中也可以,你出现了具体什么错误请写出来
      

  3.   

    sswt(sweater) 
    这就是我的具体问题:因为action中含有了servlet对象,所以我在action中直接使用了GetSmartUpload.getSmartUpload(servlet,request,response);(GetSmartUpload为一类详见下)结果显示的错误是su(SmartUpload 对象)总是空异常,能帮我看看吗我在写一个小图书管理系统的项目(供单位内部使用),因为要将书的图形信息和文本信息一起上传,所以我使用了jspsmartupload,因为,使用了jspsmartupload就不能用struts对应的formbean来获得数据了,所以我在jsp页中,对应的上传得表单的标签我没用struts的来写,这是我的jsp(add.jsp是其中的一部分)<html:form action="AddBookInformation.do" method="post" enctype="multipart/form-data">
    <td >封面图像</td>                                 
    <td ><input type="file" name="cover" ></td>
    <td>数量</td>                                
    <td><input type="text" name="total"></td>
          
    这是我获得jspsmartupload对象的java文件
    package Liberary;import  javax.servlet.*;
    import  javax.servlet.http.*;
    import  com.jspsmart.upload.*;
    import  java.util.*;
    import  java.io.*;
    import  javax.servlet.jsp.PageContext;
    import  javax.servlet.jsp.JspFactory;public class GetSmartUpload
    {
        public static SmartUpload getSmartUpload(Servlet servlet,HttpServletRequest req,HttpServletResponse res)
    {
        SmartUpload su=new SmartUpload();
        try
        {
    su.initialize(servlet.getServletConfig(),req,res);
    su.upload();
        }
        catch(Exception e)
        {
    System.out.println(e.getMessage());
        }     
        return su;
    }
    }
    当我在AddBookInformationAction.java中执行到这句话
    SmartUpload su=GetSmartUpload.getSmartUpload(servlet,request,response);
    时发现速度异常缓慢(经过测试发现的),最后进度条一动不动了,问题估计是出在getSmartUpload(servlet,request,response)上,请大家看看该方法是哪里导致了这么慢,一直后续的语句都没有执行
      

  4.   

    呵呵,我写了一个不用jspsmartupload的无组件上载的程序,把代码给你看看,不过我没有用过struts,所以不知道能不能帮你。
    package util; 
    import java.sql.*;
    import java.io.*;
    import java.util.*; 
    import javax.servlet.*; 
    import javax.servlet.http.*; public class UpLoad{
    //变量申明
    public static final int BUF_SIZE = 1024*400; //一行读入最大字节数
        public static final int STREAM_SIZE = 5 * 1024*1024;  //文件上限字节数
        protected java.util.Hashtable hashElements; // contain the parsed form data    filename-->ArrayList
        public static final String NAME_STRING = "Content-Disposition: form-data; name=\""; 
        public static final String CONTENT_TYPE_STRING = "Content-Type: ";
        public static final String Encode = "ISO-8859-1"; 
        public static final String split = "\\";
        
    /**分析输入流
     *Created by wxj on May 19, 2001
     */
    public void parser(HttpServletRequest request) throws IOException{
    String  firstLine;  //读入的第一行内容
    String  strResult;  //读入的内容
    String  strKey=null;        //上传文件变量名
    String  strContentType=null;
    UpLoadedFile upFile =null; //上传文件对象
    String strFile = "";
    String strPath = "";
    int     readLen;    //读入的字节数
    int iNameStrLen = NAME_STRING.length();  
    ServletInputStream is;
        is = request.getInputStream();   //输入流
        byte[] b= new byte[BUF_SIZE]; //
        boolean bSection = false;  //是否开始读取文件字节流
       
        hashElements = new Hashtable(); 
       
        //保留一组同变量名的UpLoadFile对象
        ArrayList values = null; 
       
        //读取第一行
        readLen = is.readLine(b,0,BUF_SIZE);
        firstLine = new String( b, 0, readLen -4);
    while (-1 != (readLen = is.readLine(b,0,BUF_SIZE)) ) {
            strResult = new String(b,0,readLen);
         //读取文件名行
         int index;  
          
         if(strResult.length() >= iNameStrLen 
            && strResult.substring(0,iNameStrLen).compareTo(NAME_STRING) == 0){
               //取变量名
            index = strResult.indexOf("\"", iNameStrLen );
            strKey = strResult.substring(iNameStrLen, index); 
           
            strResult = strResult.substring(index+1);
            index = strResult.indexOf("filename=\"");
            if(index>0 ){
            //表示改field是文件
            //读取文件名和路径
            strResult = strResult.substring(index + "filename=\"".length());
            strFile = strResult.substring(strResult.indexOf(split)+1,strResult.length()-1);
            strPath = strResult.substring(0,strResult.indexOf(split) + 1);
            upFile = new UpLoadedFile();
            hashElements.put(strKey,upFile);
            upFile.setFileName(strFile);
            upFile.setPathName(strPath);
            ByteArrayOutputStream contentStream = new ByteArrayOutputStream(STREAM_SIZE);
            upFile.setBBuffer(contentStream);
            //读取type
            readLen = is.readLine(b,0,BUF_SIZE);
            strResult = new String(b,0,readLen);
            index = strResult.indexOf(CONTENT_TYPE_STRING);
            if(index==0){
            upFile.setContentType(strResult.substring(CONTENT_TYPE_STRING.length()+1));
            }
            //跳过一行空行
            readLen = is.readLine(b,0,BUF_SIZE);
            while(-1 != (readLen = is.readLine(b,0,BUF_SIZE))){
            if(readLen < 2 * firstLine.length() ){
            strResult = new String(b,0,readLen);
            if(strResult.indexOf(firstLine)==0)
            break;
            }
            contentStream.write(b,0,readLen);
            }
            contentStream.close();
            }else{
             //跳过一行空行
            readLen = is.readLine(b,0,BUF_SIZE);
            strResult = "";
            while(-1 != (readLen = is.readLine(b,0,BUF_SIZE))){
            if(readLen < 2 * firstLine.length() ){
            if( new String(b,0,readLen).indexOf(firstLine)==0)
            break;
            }
            strResult = strResult + (new String(b,0,readLen));
            }
            hashElements.put(strKey,strResult);
            }
               }
    }
    }

    //根据变量名返回一个object对象
    public Object getParameter(String strName) { 
    Object objResult = null; 
    if ( hashElements.containsKey(strName) ) { 
    objResult = hashElements.get(strName); 


    return objResult; 
    } public Object[] getParameterValues(String strName) { 
    Object[] objResults = null; 
    if ( hashElements.containsKey(strName) ) { 
    ArrayList objList = (ArrayList)hashElements.get(strName); 
    objResults = objList.toArray(); 
    }
    return objResults; 

    //将输入流保存到指定文件
    /**@Param  is  输入流
     *@Param fileName 保存输入流内容的文件
     *@Param bOverWrite 是否覆盖
     */
    public void blobToFile(InputStream is, String fileName, boolean bOverWrite)
    throws SQLException,IOException,Exception{
    File objFile = new File(fileName ); 
         
         if ( objFile.isDirectory() ) 
    throw new Exception( "DESTINATION_IS_A_DIRECTORY" ); 

         if ( objFile.isFile() && objFile.exists() && !bOverWrite ) 
    throw new Exception( "DESTINATION_FILE_ALREADY_EXISTS" ); 

    FileOutputStream streamFileOutput = new FileOutputStream(objFile);

    byte[] b;
    int byte_read;

    b = new byte[BUF_SIZE]; 

    byte_read = is.read(b);
    if(byte_read == -1){
    byte_read =0;
      streamFileOutput.write(b,0,byte_read);
          streamFileOutput.flush();
      }
      else{
      do{
    streamFileOutput.write(b,0,byte_read);
          streamFileOutput.flush();
          }while( -1 != (byte_read = is.read(b)));
      }
     
          streamFileOutput.close();
         
    }

    /**将结果集rs中的当前行中的fld字段中的内容保存到fileName中
     *@Param rs  结果集
     *@Param fld 字段名
     *@Param fileName 文件名(包括路径)
     *@Param bOverWrite  是否覆盖
     *Created by wxj on May 19,2001
     */
    public  void fieldToFile(ResultSet rs, String fld, String fileName, boolean bOverWrite)
    throws IOException, SQLException,Exception{
    InputStream is = rs.getBinaryStream(fld);
      blobToFile(is, fileName, bOverWrite);
    }


    public  void fieldToFile(ResultSet rs, int fld, String fileName, boolean bOverWrite)
    throws IOException, SQLException,Exception{
    InputStream is = rs.getBinaryStream(fld);
      blobToFile(is, fileName, bOverWrite);
    }

    public void fieldToFile(ResultSet rs, String fld, String fileName)
    throws IOException, SQLException,Exception{
    fieldToFile(rs,fld,fileName,true);
    }

    public void fieldToFile(ResultSet rs, int fld, String fileName)
    throws IOException, SQLException,Exception{
    fieldToFile(rs,fld,fileName,true);
    }
    }
     
      

  5.   

    /* * source file: UpLoadFile.java
     * Creation date: (May 16 2001 ) 
     * @author: wxj 
     * UpLoadedFile :代表上传的文件,每一个上传的文件有一个该类的相应的对象。 
    */package util;  
    import java.io.*; 
    import java.sql.*; public class UpLoadedFile { 
    public static final String DESTINATION_FILE_ALREADY_EXISTS = "文件已存在."; 
    public static final String DESTINATION_IS_A_DIRECTORY = "The destination is a directory."; 
    protected java.lang.String strPathName; // 上传文件的路径
    protected java.lang.String strFileName; // the upLoaded file name 
    protected java.lang.String strContentType;  // content type of the upLoaded file name 
    protected java.io.ByteArrayOutputStream bBuffer;  // file's content in byte format --- OutputStream.

    /** 
      * file's content in byte formate   
      * Creation date: (May 16,2001 ) by wxj 
      * @return java.io.ByteArrayOutputStream 
      */ 
    public ByteArrayOutputStream getBuffer() { 
    return bBuffer; 
    }

    /** 
      * get file's content type 
       * @return java.lang.String 
      */ 
    public java.lang.String getStrContentType() { 
    return strContentType; 
    }
     
    /** 
      * Insert the method's description here. 
      * Creation date: (3/15/2001 10:27:37 AM) 
      * @param newBBuffer java.io.ByteArrayOutputStream 
      */ 
    void setBBuffer(ByteArrayOutputStream buffer) { 
    bBuffer = buffer; 
    }  /** 
       * @param newStrContentType java.lang.String 
      */ 
    public void setContentType(java.lang.String newStrContentType) { 
    strContentType = newStrContentType; 
    }  /**
    *get upFile's contentType
    */
    public java.lang.String getContentType() { 
    return strContentType;
    }

    /** 
      * get file's name 
      * @return java.lang.String 
      */ 
    public java.lang.String getFileName() { 
    return strFileName; 
    }

    /** 
      * @param newStrFileName java.lang.String 
      */ 
    public void setFileName(java.lang.String newStrFileName) { 
    strFileName = newStrFileName; 
    }

    /** 
      * get file's path
      * @return java.lang.String 
      */ 
    public java.lang.String getPathName() { 
    return strPathName; 
    }  /** 
      * @param newStrFileName java.lang.String 
      */  
    public void setPathName(java.lang.String pathName) { 
    strPathName = pathName; 
    }


    /** 
      * Save the upLoaded file's content to a local file 
      * @param strFileName java.lang.String 
      * destination's full path name. 
      * @param bOverWrite boolean 
      * true: overwrite it if the destination already exists. 
      * false: do not overwrite it. 
      * @exception java.lang.Exception The exception description. 
      * Note: if the file's content stream is null, it will create an empty file 
      * for you. 
      */ 
    public void writeToFile(String strFileName, boolean bOverWrite)  
    throws java.lang.Exception  

    File objFile = new File(strFileName); 

    if ( objFile.isDirectory() ) 
    throw new Exception( DESTINATION_IS_A_DIRECTORY ); 

    if ( objFile.isFile() && objFile.exists() && !bOverWrite ) 
    throw new Exception( DESTINATION_FILE_ALREADY_EXISTS );  FileOutputStream streamFileOutput = new FileOutputStream(objFile); 
    //bBuffer.writeTo(streamFileOutput); 
    streamFileOutput.write(bBuffer.toByteArray(),0,bBuffer.size() -2);
    bBuffer.flush(); 
    streamFileOutput.flush(); 
    bBuffer.close(); 
    streamFileOutput.close(); 


    public void writeToFile(String strFileName)  
    throws java.lang.Exception {
    writeToFile(strFileName,true);
    } public void setToField(PreparedStatement pstmt,int fld)
    throws SQLException{
    java.io.ByteArrayInputStream inBBuffer;
    inBBuffer = new ByteArrayInputStream(bBuffer.toByteArray());
    pstmt.setBinaryStream(fld, inBBuffer, bBuffer.size() -2);
    }
    }
      

  6.   

    jsp页面
    <%@page import="util.*"%>
    <%
    UpLoad upload = new UpLoad();
    upload.parser(request);
    out.print((String)upload.getParameter("a") +"<br>");
    out.print((String)upload.getParameter("b") +"<br>");
    UpLoadedFile f = (UpLoadedFile)upload.getParameter("c");
    f.writeToFile("c:\\c.txt");
    %>ok
      

  7.   

    谢谢 ytrgmj(一拖死),不过现在还不打算结贴,看看还有没有高人能解决我的问题