在jsp中使用smartupload組件上傳檔案ggg

來源:互聯網
上載者:User

 jsp對上傳檔案的支援不象php中支援的那麼好,直接做成了函數,也不象asp中要通過組件才能實現。jsp中可以通過javabean來實現。但是我們沒有必要自己去寫一個上傳的bean,在網上已經有了很多成型的技術,smartupload就是其中的一個。但是smartupload是將檔案先讀到伺服器的記憶體中,所以上傳太大的檔案(超過100兆)有可能會出問題,也算是一個美中不足吧:)

   先說一下提交的頁面,smartupload組件要求用位元組流的方式來提交<FORM action="upload.jsp"  encType=multipart/form-data method=post>。下面就是個例子upload.htm:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0057)http://localhost:8080/jspsmartfile/jsp/uploadTemplate.jsp -->
<HTML><HEAD>
<META content="text/html; charset=gb2312" http-equiv=Content-Type>
<META content="MSHTML 5.00.2920.0" name=GENERATOR></HEAD>
<BODY bgColor=#e6e6e6><BR>
<FORM action="upload.jsp"  encType=multipart/form-data method=post>
<TABLE>
  <TBODY>
  <TR>
    <TD><FONT color=#000000 face=helv,helvetica size=1>&nbsp;&nbsp;File 
      :&nbsp;</FONT>&nbsp;&nbsp;<INPUT  size=60 type=file  name="file"></TD></TR>
        <TR>
    <TR>
    <TD><FONT color=#000000 face=helv,helvetica size=1>&nbsp;&nbsp;File 
      :&nbsp;</FONT>&nbsp;&nbsp;<INPUT  size=60 type=file  name="file1"></TD></TR>
        <TR>  
    <TD><FONT color=#000000 face=helv,helvetica size=1>&nbsp;&nbsp;File 
      :&nbsp;</FONT>&nbsp;&nbsp;<INPUT  size=60 type=text  name="text"></TD></TR>
  <TR>
    <TD
align=right><INPUT type=submit value=Send name="send"></TD></TR></TBODY></TABLE></FORM></BODY></HTML>

  再來看一下接收的頁面 ,我們把檔案上傳到伺服器以後就直接把它再存入資料庫中:upload.jsp

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="com.jspsmart.upload.*" %>
<%@ page import="DBstep.iDBManager2000.*"%>
<%
   //執行個體化上傳bean
    com.jspsmart.upload.SmartUpload mySmartUpload=new com.jspsmart.upload.SmartUpload();
    //初始化
    mySmartUpload.initialize(pageContext); 
    //設定上傳的最大值
    mySmartUpload.setMaxFileSize(500 * 1024*1024);
    //上傳檔案
    mySmartUpload.upload();
   //迴圈取得所有上傳的檔案
   for (int i=0;i<mySmartUpload.getFiles().getCount();i++){
   //取得上傳的檔案
   com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
   if (!myFile.isMissing())
    {
   //取得上傳的檔案的檔案名稱
    String myFileName=myFile.getFileName();
    //取得不帶尾碼的檔案名稱
    String  suffix=myFileName.substring(0,myFileName.lastIndexOf('.'));
    //取得尾碼名
    String  ext= mySmartUpload.getFiles().getFile(0).getFileExt();  
    //取得檔案的大小 
    int fileSize=myFile.getSize();
    //儲存路徑
    String aa=getServletContext().getRealPath("/")+"jsp//";
    String trace=aa+myFileName;
    //取得別的參數
    String explain=(String)mySmartUpload.getRequest().getParameter("text");
    String send=(String)mySmartUpload.getRequest().getParameter("send");
    //將檔案儲存在伺服器端
    myFile.saveAs(trace,mySmartUpload.SAVE_PHYSICAL);
    //下面的是將上傳的檔案儲存到資料庫中
    //將檔案讀到流中
    java.io.File file = new java.io.File(trace);
    java.io.FileInputStream fis = new java.io.FileInputStream(file);
  out.println(file.length());
   //開啟資料庫
   ResultSet result=null;
   String mSql=null;
   PreparedStatement prestmt=null;
   DBstep.iDBManager2000 DbaObj=new DBstep.iDBManager2000();
   DbaObj.OpenConnection();
   //將檔案寫到資料庫中
   mSql="insert into marklist (markname,password,marksize,markdate,MarkBody) values (?,?,?,?,?)";
   prestmt =DbaObj.Conn.prepareStatement(mSql);
   prestmt.setString(1, "aaa1");
   prestmt.setString(2, "0000");
   prestmt.setInt(3, fileSize);
   prestmt.setString(4, DbaObj.GetDateTime());
   prestmt.setBinaryStream(5,fis,(int)file.length());
   DbaObj.Conn.setAutoCommit(true) ;
   prestmt.executeUpdate();
   DbaObj.Conn.commit();
   out.println(("上傳成功!!!").toString());
   }
   else
   { out.println(("上傳失敗!!!").toString()); }
   }//與前面的if對應
%>

   再說一下下載,下載分兩種情況1。從資料庫直接下載2。從伺服器上下載

  先說從資料庫直接下載的情形:就是把輸入資料流從資料庫裡讀出來,然後轉存為檔案

<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*" %>
<%@ page import="DBstep.iDBManager2000.*"%>
<%
    int bytesum=0;
    int byteread=0;
  //開啟資料庫
  ResultSet result=null;
  String Sql=null;
  PreparedStatement prestmt=null;
  DBstep.iDBManager2000 DbaObj=new DBstep.iDBManager2000();
  DbaObj.OpenConnection();
 //取得資料庫中的資料
 Sql="select  *  from  t_local_zhongzhuan ";
 result=DbaObj.ExecuteQuery(Sql);
 result.next();

 //將資料庫中的資料讀到流中
InputStream inStream=result.getBinaryStream("content");
FileOutputStream fs=new FileOutputStream( "c:/dffdsafd.doc");

  byte[]  buffer =new  byte[1444];
int length;
    while ((byteread=inStream.read(buffer))!=-1)
    {
       out.println("<DT><B>"+byteread+"</B></DT>");
       bytesum+=byteread;
       System.out.println(bytesum);
   
   
       fs.write(buffer,0,byteread);
     }
%>

再說從伺服器上下載的情形:

<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.io.*" %>
<%
  String fileName = "zsc104.swf".toString();
f//讀到流中
InputStream inStream=new FileInputStream("c:/zsc104.swf");
 //設定輸出的格式
  response.reset();
  response.setContentType("bin");
  response.addHeader("Content-Disposition","attachment; filename=/"" + fileName + "/"");
 //迴圈取出流中的資料
  byte[] b = new byte[100];
  int len;
  while((len=inStream.read(b)) >0)
  response.getOutputStream().write(b,0,len);  
  inStream.close();
%>

   好了,到這裡只要不是太大的檔案的上傳下載的操作都可以完成了

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.