jsp上传图片怎么解决啊,就是上传后保存到一个文件加中,同时获取图片的路径,谢谢啦

解决方案 »

  1.   

    用SmartUpload处理的,供参考<%-- 
    文件名:do_upload.jsp 
    --%> 
    <%@ page contentType="text/html; charset=gb2312" language="java" 
    import="com.jspsmart.upload.*,java.sql.*" 
    errorPage=""%> 
    <html> 
    <head> 
    <title>文件上传处理页面</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
    </head> <body> 
    <%
    Connection con;
    ResultSet rs;
    Statement stmt;      
          //数据库登录用户和密码
          String user="sa";
          String password="sa";
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          String url="jdbc:odbc:jspbook";
          con=DriverManager.getConnection(url,user,password); 
          //创建一个jdbc声明 
          stmt=con.createStatement();       
    SmartUpload su = new SmartUpload(); 
    su.initialize(pageContext); 
    su.upload(); 
    String title=su.getRequest().getParameter("title"); 
    String Re=su.getRequest().getParameter("Re");int count = su.save("/upload"); 
    out.println(count+"个文件上传成功!<br>"); 
    //out.println("TEST="+su.getRequest().getParameter("TEST")+"<BR><BR>"); 
    System.out.println("2");
    for (int i=0;i<su.getFiles().getCount();i++) 

    System.out.println("3");
    com.jspsmart.upload.File file = su.getFiles().getFile(i); if (file.isMissing()) continue; String filename=file.getFileName();
    int ifilelength=file.getSize();
    String sfilelength=Integer.toString(ifilelength);
    String filetype=file.getFileExt();
    java.util.Date  dt = new java.util.Date();
    String suploadtime=String.valueOf(1900+dt.getYear())+"-"+String.valueOf(1+dt.getMonth())
                      +"-"+String.valueOf(dt.getDate());
    stmt.executeUpdate("insert into uploadfileinfo(filename,title,"
                     +"filetype,fiellength,uploadtime,description)"
                     +"values('"+filename+"','"+title+"','"+filetype+"','"
                     +sfilelength+"','"+suploadtime+"','"+Re+"')");
    out.println("<TABLE BORDER=1>"); 
    out.println("<TR><TD>文件名:</TD><TD>"+ filename + "</TD></TR>"); 
    out.println("<TR><TD>文件长度:</TD><TD>" +sfilelength + "</TD></TR>"); 
    out.println("<TR><TD>文件类型:</TD><TD>"+ filetype + "</TD></TR>"); 
    //out.println("<TR><TD>文件全名(FilePathName)</TD><TD>"+ file.getFilePathName() + "</TD></TR>"); 
    out.println("</TABLE><BR>"); 

    %> 
    </body> 
    </html>
      

  2.   

    // Initialization
        su.initialize(config, request, response);
    // 上傳初始化
    su.initialize(pageContext);
    //1 .限制上傳文件的最大長度
    su.setMaxFileSize(500*1024*1024);
    su.setTotalMaxFileSize(500*1024*1024);
    //2 .設定允許上傳的文件
    su.setAllowedFilesList("doc,txt,html,htm,jpg,jpeg,xls,gif"); 
    // 3.設定禁止上傳的文件
    su.setDeniedFilesList("exe,bat,,");
    // 上傳文件
    su.upload();
      

  3.   

    忘了﹗SmartUpload su = new SmartUpload();
    放前面﹗﹗
      

  4.   

    能否给我一个jspsmartupload,谢谢!
    [email protected]
      

  5.   

    发过去了<%@ page contentType="text/html;charset=GBK" %>
    <%@ page language="java" import="com.jspsmart.upload.*"%>
    <%@ page import="java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*" %>
    <jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" /><HTML>
      <BODY BGCOLOR="white">
        <%
        //初始化文件名和上传结果
        String FileName = "noImage";
        String result ="error";
        try
        {
          // 执行初始化操作
          mySmartUpload.initialize(pageContext);
          //限制文件上传格式
          mySmartUpload.setAllowedFilesList("jpg,gif,bmp,jpeg,JPG,BMP,GIF");
          // 上传文件到服务器
          mySmartUpload.upload();      //如果有文件上传
          if(mySmartUpload.getFiles().getCount()>0)
          {
            // 取出文件
            com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
            //
            //如果文件存在
            if(myFile.getSize()>0)
            {
              String strExt = myFile.getFileExt();
              java.util.Date d = new java.util.Date();
              FileName = d.getTime()+ "."+strExt;
              myFile.saveAs("../webblog/images/upload/articleImage/" +FileName);
              result="success";          //以下生成缩略图
              String strRealPath = request.getRealPath("/")+"\\images\\upload\\articleImage\\"+FileName; //原图路径
              java.io.File file = new java.io.File(strRealPath);        //读入刚才上传的文件
              String newUrl=request.getRealPath("/")+"\\images\\upload\\articleImage\\small\\"+FileName;  //新的缩略图保存地址
              Image src = javax.imageio.ImageIO.read(file);                     //构造Image对象
              float tagsize=300;
              int old_w=src.getWidth(null);                                     //得到源图宽
              int old_h=src.getHeight(null);   //得到源图长
              int tempsize;
              float tempdouble = 1;
              if(old_w > 300 || old_h>300){
                if(old_w>old_h){
                  tempdouble=old_w/tagsize;
                }else{
                  tempdouble=old_h/tagsize;
                }
              }
              int new_w =Math.round(old_w/tempdouble);
              int new_h=Math.round(old_h/tempdouble);//计算新图长宽
              BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
              tag.getGraphics().drawImage(src,0,0,new_w,new_h,null);       //绘制缩小后的图
              FileOutputStream newimage=new FileOutputStream(newUrl);          //输出到文件流
              JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
              encoder.encode(tag);                                               //近JPEG编码
              newimage.close();
            }
            else
            {
              System.out.println("文件不存在!");
              //result="error";
            }
          }
          else
          {
            System.out.println("没上传!");
            //result="error";
          }
        }
        catch(Exception ex)
        {
          System.out.println(ex);
          //result="error";
        }
        finally
        {
          //存储表单信息
          String Atitle = mySmartUpload.getRequest().getParameter("Atitle");
          String Acontent = mySmartUpload.getRequest().getParameter("Acontent");
          String Atype = mySmartUpload.getRequest().getParameter("Atype");
          String Astate = mySmartUpload.getRequest().getParameter("Astate");
          request.setAttribute("Atitle",Atitle);
          request.setAttribute("Acontent",Acontent);
          request.setAttribute("Atype",Atype);
          request.setAttribute("Astate",Astate);
          request.setAttribute("FileName",FileName);
          request.setAttribute("Result",result);
        }
        %>
        <jsp:forward page="../insertarticlesrv"/>
      </BODY>
    </HTML>