jspsmartupload如果需要,给我发信,我把包发给你[email protected],或者去bj.163.com中的jsp版看看

解决方案 »

  1.   

    <tr>
                  <td width="24%" align="right" height="12" bgcolor="#FFFFFF">上传图片:</td>
                  <td width="76%" height="12" bgcolor="#FFFFFF"><input type="file" name="File1" size="20"></td>
                </tr>加进去就可以啦!呵呵!
    至于获取嘛!呵呵!加入下面代码!
    <% 
    // Variables 
    int count=0;         
      // 初始化
    mySmartUpload.initialize(pageContext); 

      //允许上传文件类型
      //mySmartUpload.setAllowedFilesList("gif,GIF,jpg,JPG"); 

    //允许上传文件的大小
    //mySmartUpload.setMaxFileSize(20000);

    //文件上传
    mySmartUpload.upload(); 
     
    //选择多个文件
    java.sql.Statement stmt=newsbean.conn.createStatement();
    int intGif=0;
    String sql=new String();
    for (int i=0;i<mySmartUpload.getFiles().getCount();i++)

     
    // Retreive the current file 
    com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i); 
      sql="select * from content order by id desc limit 1";
     
    // Save it only if this file exists 
    if (!myFile.isMissing()) 

    //另外保存文件
    try{
    String sqllock="lock tables content write"; //加锁
    stmt.executeUpdate(sqllock);
    }catch(java.sql.SQLException ex){
    stmt.close();
    newsbean.conn.close();
    %>
    <script language="javascript">
    alert("操作失败!");
    history.back();
    </script>     
    <%
    out.close();
    }
      
    java.sql.ResultSet rs=stmt.executeQuery(sql);
    if(!rs.next()){
    intGif=1;
    }else{
    StringBuffer strGif=new StringBuffer("");
    String temp=rs.getString("up_photo");
    strGif.append(temp.substring(4,(temp.length()-4)));
    temp=new String(strGif);

    intGif=java.lang.Integer.parseInt(temp);
    intGif=intGif+1; }
    //}

    String strFile="NEWS"+intGif+".gif";

    int size=myFile.getSize();
    String image=myFile.getContentType();

    if(!(image.trim().equals("image/gif") || image.trim().equals("image/pjpeg")))
    {
    stmt.close();
    newsbean.conn.close();
    %>
    <script language="javascript">
    alert("图片文件必须为gif或jpg文件!");
    </script>
    <script language="javascript">
    history.back();
    </script>     
    <%
    out.close();
    }
    if(size>40000)
    {

    stmt.close();
    newsbean.conn.close();
    %>
    <script language="javascript">
    alert("图片文件大小不能超过40K");
    </script>
    <script language="javascript">
    history.back();
    </script>     
    <%
    out.close();
    }

    //上传
    myFile.saveAs("/news/image/"+strFile,mySmartUpload.SAVE_VIRTUAL);   }
      

  2.   

    你可以试试用Struts的来上传图像,不过,但是不能直接不能上船文件名或者类型,不过搞几个隐含变量,用JS就能搞定了,不过还得写FormBean和Actionbean 就稍微显者麻烦些了.这东西我刚学的,写得有些糙,凑合着看看吧:
    ********PictureFile.jsp***********************************<%@ taglib uri="/struts-bean" prefix="bean" %>
    <%@ taglib uri="/struts-html" prefix="html" %>
    <%@ taglib uri="/struts-template" prefix="template" %>
    <%@ taglib uri="/struts-logic" prefix="logic" %>
    <%@ taglib uri="/struts-html" prefix="html" %>
    <HTML>
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    <TITLE>Product File</TITLE>
    </HEAD>
    <BODY>
    <logic:present name="FileInfo" >
    <form action="/ProductFile" name=ProductFile  enctype="multipart/form-data" method="post">
    <TABLE cellSpacing=1 cellPadding=1 border=0 width=80%>
    <TABLE cellSpacing=1 cellPadding=1 border=1 width=100%>
    <TR>
    <TD align=middle width="20%">Filename</TD>
    </TR>
    <TR>
    <TD align=right width="20%">
    <html:file name="FileInfo" property="pictureData1" title="Browse File" />
    </TD>
    </TR>
    </TABLE>
    <BR><P>
    <TABLE cellSpacing=0 cellPadding=0 border=0 width=100%>
    <TR>
    <TD colspan=4 align=right>
    &nbsp;<INPUT type=button name="btnSave" value=Update onclick="doAction('Update');">
    </TD>
    </TR>

    </TABLE>
    </TABLE>
    </form>
    </logic:present></BODY>
    </HTML>
    <SCRIPT LANGUAGE=JavaScript>
    <!--function doAction(type){
    if(type=='Update'){
    str = "ProductFile.do?mode=update";
    window.ProductFile.action = str;
    window.ProductFile.submit();
    return true;
    }
    return false;
    }
    //-->
    </SCRIPT>
    ************************PictureForm.java***********************
    package Picture.form;import org.apache.struts.action.ActionForm;
    import org.apache.struts.upload.FormFile;
    import java.io.InputStream;
    public class PictureForm extends ActionForm{
      transient protected FormFile pictureData1 = null;
      public FormFile getPictureData1(){
        return this.pictureData1;
      }
      public void setPictureData1(FormFile pData){
        this.pictureData1 = pData;
      }
      public PictureForm() {}
    }
    *************************************PictureAction.java**************************
    package Picture.action;import java.util.Vector;
    import java.util.Map;
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.ActionForm;
    import java.io.IOException;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.struts.upload.FormFile;
    import java.io.InputStream;public class PictureAction extends Action{  public PictureAction() {
      }  public ActionForward perform(ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws IOException,ServletException
      {
        String mode=null;
        mode= request.getParameter("mode");
            if (mode.equals("update"))
            {
              UpdateFileDetails(request,form);
            }
        return(mapping.findForward("success"));
      }  private void UpdateFileDetails(HttpServletRequest request, ActionForm form){    try
        {
          ProdFileForm fForm = (ProdFileForm)form;
          FormFile small=fForm.getPictureData1();
          byte[] smallStream=getFileContent(small);
        }
        catch(Exception e)
        {
          System.err .println(e.getMessage() );
        }
      }
       private static byte[] getFileContent(FormFile file) throws IOException
       {
         InputStream in=file.getInputStream();
         int count=in.available();
         byte[] ret=new byte[count];
         in.read(ret);
         return ret;
       }
    }
    *****************************weblogic 的配置文件***********************
    **************\web-inf\struts-config.xml*********************
    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <!DOCTYPE struts-config PUBLIC
              "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
              "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd"><struts-config>
      <form-beans>
             <form-bean name="ProdFileForm"
                 type="com.bexcom.bexcatalog.web.product.form.ProdFileForm"/>
      </form-beans>
      <action-mappings>
         <action  path="/Picture"
                  type="PictureAction"
                  name="PictureForm"
                  scope="request"
               validate="false">
         <forward name="success"  path="/product/ProductFile.jsp"/>
        </action>
      </action-mappings>
    </struts-config>**************\web-inf\web.xml*********************
    (这个文件不用写了,下载Struts的包时,也就是那个样子,不用怎么改就可以用了)