溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

jsp中如何使用smartupload組件上傳文件

發(fā)布時間:2021-08-12 11:36:16 來源:億速云 閱讀:136 作者:Leah 欄目:編程語言

jsp中如何使用smartupload組件上傳文件,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

在JSP中使用smartUPLOAD組件上傳文件

  jsp對上傳文件的支持不象PHP中支持的那么好,直接做成了函數(shù),也不象ASP中要通過組件才能實現(xiàn)。jsp中可以通過Javabean來實現(xiàn)。但是我們沒有必要自己去寫一個上載的bean,在網(wǎng)上已經(jīng)有了很多成型的技術(shù),smartupload就是其中的一個。但是smartupload是將文件先讀到服務(wù)器的內(nèi)存中,所以上傳太大的文件(超過100兆)有可能會出問題,也算是一個美中不足吧:)

  先說一下提交的頁面,smartupload組件要求用字節(jié)流的方式來提交

。下面就是個例子upload.htm:


<!-- saved from url=(0057)http://localhost:8080/jspsmartfile/jsp/uploadTemplate.jsp --&gt







 
 
 
 
 
 
  
 
 
 <TD
align=right>
  File 
  : 
  

  File 
  : 
  
  File 
  : 
  

undefined

  再來看一下接收的頁面 ,我們把文件上傳到服務(wù)器以后就直接把它再存入數(shù)據(jù)庫中: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); 
  //設(shè)置上載的最大值
  mySmartUpload.setMaxFileSize(500 * 1024*1024);
  //上載文件
  mySmartUpload.upload();
  //循環(huán)取得所有上載的文件
  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;
  //取得別的參數(shù)
  String explain=(String)mySmartUpload.getRequest().getParameter("text");
  String send=(String)mySmartUpload.getRequest().getParameter("send");
  //將文件保存在服務(wù)器端
  myFile.saveAs(trace,mySmartUpload.SAVE_PHYSICAL);
  //下面的是將上載的文件保存到數(shù)據(jù)庫中
  //將文件讀到流中
  java.io.File file = new java.io.File(trace);
  java.io.FileInputStream fis = new java.io.FileInputStream(file);
  out.println(file.length());
  //打開數(shù)據(jù)庫
  ResultSet result=null;
  String mSql=null;
  PreparedStatement prestmt=null;
  DBstep.iDBManager2000 dbaObj=new DBstep.iDBManager2000();
  DbaObj.OpenConnection();
  //將文件寫到數(shù)據(jù)庫中
  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對應(yīng)
%>

  再說一下下載,下載分兩種情況1。從數(shù)據(jù)庫直接下載2。從服務(wù)器上下載

  先說從數(shù)據(jù)庫直接下載的情形:就是把輸入流從數(shù)據(jù)庫里讀出來,然后轉(zhuǎn)存為文件

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

 //將數(shù)據(jù)庫中的數(shù)據(jù)讀到流中
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("

  • "+byteread+"

  • ");


  •   bytesum+=byteread;


  •   System.out.println(bytesum);


  •  


  •  


  •   fs.write(buffer,0,byteread);


  •   }


  • %>

再說從服務(wù)器上下載的情形:

<%@ page="" contenttype="text/html; charset=gb2312">
<%@ page="" import="java.io.*">
<%
  String fileName = "zsc104.swf".toString();
f//讀到流中
InputStream inStream=new FileInputStream("c:/zsc104.swf");
 //設(shè)置輸出的格式
  response.reset();
  response.setContentType("bin");
  response.addHeader("Content-Disposition","attachment; filename="" + fileName + """);
 //循環(huán)取出流中的數(shù)據(jù)
  byte[] b = new byte[100];
  int len;
  while((len=inStream.read(b)) >0)
  response.getOutputStream().write(b,0,len);  
  inStream.close();
%>

看完上述內(nèi)容,你們掌握jsp中如何使用smartupload組件上傳文件的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI