JSP servlet實(shí)現(xiàn)文件上傳下載和刪除

本文實(shí)例為大家分享了android九宮格圖片展示的具體代碼,供大家參考,具體內(nèi)容如下

由于存儲(chǔ)空間,對(duì)一般用戶而言,就是用來操作存儲(chǔ)文件的,所以這兩天,特意看了一下windows下用servlet實(shí)現(xiàn)文件上傳、下載和刪除,下面是詳細(xì)代碼說明

上傳:

用的是commons-fileupload-1.2.2.jar和commons-io-2.0.1.jar組件,可以去apache官網(wǎng)上去下載,然后放到webroot/web-inf/lib目錄下 

upload.html

 <html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=gbk"> 
  <title>file upload</title> 
</head> 
<body> 
<!-- action="fileupload"對(duì)應(yīng)web.xml中<servlet-mapping>中<url-pattern>的設(shè)置.--> 
<!-- 必須是multipart的表單數(shù)據(jù),才能完整的傳遞文件數(shù)據(jù) --> 
  <form name="myform" action="fileupload" method="post" 
    enctype="multipart/form-data"> 
    file:<br> 
    <input type="file" name="myfile"><br> 
    <br> 
    <input type="submit" name="submit" value="commit"> 
  </form> 
</body> 
</html> 

web.xml里加上下面幾行:

<servlet> 
 <servlet-name>upload</servlet-name> 
 <servlet-class>am.demo.upload</servlet-class> 
 </servlet> 
 <servlet-mapping> 
 <servlet-name>upload</servlet-name> 
 <url-pattern>/fileupload</url-pattern> 
 </servlet-mapping> 

src目錄下新建文件upload.java:

package am.demo; 
 
import java.io.file; 
import java.io.ioexception; 
import java.util.iterator; 
import java.util.list; 
 
import javax.servlet.servletexception; 
import javax.servlet.http.httpservlet; 
import javax.servlet.http.httpservletrequest; 
import javax.servlet.http.httpservletresponse; 
 
import org.apache.commons.fileupload.fileitem; 
import org.apache.commons.fileupload.disk.diskfileitemfactory; 
import org.apache.commons.fileupload.servlet.servletfileupload; 
 
@suppresswarnings("serial") 
public class upload extends httpservlet { 
  private string uploadpath = "d://temp"; // 上傳文件的目錄 
 
  @suppresswarnings("unchecked") 
  public void dopost(httpservletrequest request, httpservletresponse response) 
      throws ioexception, servletexception { 
    try { 
      // create a factory for disk-based file items 
      diskfileitemfactory factory = new diskfileitemfactory(); 
 
 
      // create a new file upload handler 
      servletfileupload upload = new servletfileupload(factory); 
 
 
      list<fileitem> items = upload.parserequest(request);// 得到所有的文件 
      iterator<fileitem> i = items.iterator(); 
      while (i.hasnext()) { 
        fileitem fi = (fileitem) i.next(); 
        string filename = fi.getname(); 
        if (filename != null) { 
          file fullfile = new file(fi.getname()); 
          file savedfile = new file(uploadpath, fullfile.getname()); 
          fi.write(savedfile); 
        } 
      } 
       
      response.setcontenttype("text/html;charset=gbk");  
      response.getwriter().print( 
      "<mce:script language='javascript'><!-- 
alert('上傳成功');window.location.href='index.jsp'; 
// --></mce:script>"); 
    } catch (exception e) { 
      // 可以跳轉(zhuǎn)出錯(cuò)頁面 
      e.printstacktrace(); 
    } 
  } 
 
  public void init() throws servletexception { 
    file uploadfile = new file(uploadpath); 
    if (!uploadfile.exists()) { 
      uploadfile.mkdirs(); 
    } 
  } 
} 

再看下載downlaod.java:

package am.demo; 
 
import java.io.file; 
import java.io.fileinputstream; 
import java.io.ioexception; 
import java.io.outputstream; 
 
import javax.servlet.servletexception; 
import javax.servlet.http.httpservlet; 
import javax.servlet.http.httpservletrequest; 
import javax.servlet.http.httpservletresponse; 
 
@suppresswarnings("serial") 
public class download extends httpservlet { 
  public void doget(httpservletrequest request, httpservletresponse response) 
      throws ioexception, servletexception { 
 
 
    string afilename = new string(request.getparameter("name").getbytes( 
        "iso8859-1"), "gbk"); 
 
    file fileload = new file("d:/temp", afilename); 
 
    fileinputstream in = null; // 輸入流 
    outputstream out = response.getoutputstream(); 
    byte b[] = new byte[1024]; 
 
    try { 
 
       response.setcontenttype("application/x-msdownload;"); 
 
      response.setheader("content-disposition", "attachment; filename=" 
          + new string(afilename.getbytes("gbk"), "iso-8859-1")); 
 
      // download the file. 
      in = new fileinputstream(fileload); 
      int n = 0; 
      while ((n = in.read(b)) != -1) { 
        out.write(b, 0, n); 
      } 
 
    } catch (throwable e) { 
      e.printstacktrace(); 
    } finally { 
      try { 
        in.close(); 
        out.close(); 
      } catch (throwable e) { 
        e.printstacktrace(); 
      } 
    } 
  } 
 
  public void dopost(httpservletrequest request, httpservletresponse response) 
      throws ioexception, servletexception { 
    doget(request, response); 
  } 
 
} 

刪除delete.java:

package am.demo; 
 
import java.io.file; 
import java.io.filenotfoundexception; 
import java.io.ioexception; 
 
import javax.servlet.servletexception; 
import javax.servlet.http.httpservlet; 
import javax.servlet.http.httpservletrequest; 
import javax.servlet.http.httpservletresponse; 
 
@suppresswarnings("serial") 
public class delete extends httpservlet { 
 
  public void doget(httpservletrequest request, httpservletresponse response) 
      throws filenotfoundexception, ioexception { 
 
    string afilename = new string(request.getparameter("name").getbytes( 
        "iso8859-1"), "gbk"); 
 
    file file = new file("d:/temp", afilename); 
 
    response.setcontenttype("text/html;charset=gbk"); 
 
    if (!file.isdirectory()) { 
      file.delete(); 
      response.getwriter().print( 
        "<mce:script language='javascript'><!-- 
alert('刪除成功');window.location.href='index.jsp'; 
// --></mce:script>"); 
    } else { 
 
    } 
 
  } 
 
  public void dopost(httpservletrequest request, httpservletresponse response) 
      throws ioexception, servletexception { 
    doget(request, response); 
  } 
} 

 因?yàn)閡buntu server是黑屏,本人用的是ubuntu server10.04,不方便用瀏覽器來查看文件,因?yàn)闆]有圖形界面,也不方便調(diào)試,所以可以先在windows上測(cè)試通過,然后把相應(yīng)文件拷到ubuntu server中的tomcat相應(yīng)目錄下,可以通過samba來傳文件,但遇到一個(gè)問題,就是中文亂碼的問題,因?yàn)樵陂_始安裝時(shí),選的是英文,后面用網(wǎng)上的一些辦法,還是沒有解決中文亂碼的問題,有知道的大俠,還煩請(qǐng)轉(zhuǎn)告。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持碩編程。

相關(guān)文章
亚洲国产精品第一区二区,久久免费视频77,99V久久综合狠狠综合久久,国产免费久久九九免费视频