java 檔案的上傳和下載

來源:互聯網
上載者:User

標籤:foreach   val   cache   html 4.01   post   orm   slist   forward   ack   

主要介紹使用 smartupload.jar 包中的方法對檔案的上傳和下載。上傳時檔案是存放在伺服器中,我用的是tamcat。

首先建立一個servlet 類,對檔案的操作

 1 package com.dkt.servlet; 2  3 import java.io.File; 4 import java.io.IOException; 5 import java.sql.SQLException; 6 import java.util.ArrayList; 7  8 import javax.servlet.ServletException; 9 import javax.servlet.http.HttpServlet;10 import javax.servlet.http.HttpServletRequest;11 import javax.servlet.http.HttpServletResponse;12 13 import org.lxh.smart.Files;14 import org.lxh.smart.SmartUpload;15 import org.lxh.smart.SmartUploadException;16 17 public class FileUpdownLoad extends HttpServlet {18 19     public void doGet(HttpServletRequest request, HttpServletResponse response)20             throws ServletException, IOException {21         doPost(request, response);22     }23 24     public void doPost(HttpServletRequest request, HttpServletResponse response)25             throws ServletException, IOException {26 27         response.setContentType("text/html");28         request.setCharacterEncoding("GB2312");29         response.setCharacterEncoding("GB2312");30         SmartUpload su = new SmartUpload();31         32         String op = request.getParameter("op");33         if (("upload").equals(op)) {34             su.initialize(getServletConfig(), request, response);//初始化35             su.setAllowedFilesList("txt,jpg,png,docx,xml,PNG,xlsx");//允許上傳的檔案類型,區分大小寫36             try {37                 su.setDeniedFilesList("html,exe");//不允許上傳的檔案類型38             } catch (SQLException e) {39                 e.printStackTrace();40             }41             su.setMaxFileSize(5000000);//檔案大小42             43             /*Files files = su.getFiles();44             org.lxh.smart.File file2 = files.getFile(0);45             System.out.println(file2.getFileName());46             for(int i=0;i<files.getCount();i++){47                 org.lxh.smart.File file = files.getFile(i);48                 String fileName = file.getFileName();49                 System.out.println("fileName---->"+fileName);50                 fileName =  new String(fileName.getBytes("ISO-8859-1"),"GB2312");51                 try {52                     file.saveAs(fileName);53                 } catch (SmartUploadException e) {54                     e.printStackTrace();55                 }56             }*/57             try {58                 su.upload();//上傳59                 su.save("/smartUpdownload");//上傳的路徑,在tomcat下的檔案夾中60                 System.out.println("上傳成功!!!");61             } catch (SmartUploadException e) {62                 e.printStackTrace();63             }64             request.getRequestDispatcher("fileUpDownload.jsp").forward(request, response);65         }else if(("showFiles").equals(op)){66             //得到上傳到伺服器的檔案夾67             String realPath = this.getServletContext().getRealPath("smartUpdownload");68             //得到所有檔案的檔案名稱,並顯示在頁面上69             File file = new File(realPath);70             File[] listFiles = file.listFiles();71             ArrayList<String> list = new ArrayList<String>();72             for (File fi : listFiles) {73                 list.add(fi.getName());//把所有的檔案名稱存入集合74             }75             request.setAttribute("list", list);        76             request.getRequestDispatcher("fileUpDownload.jsp").forward(request, response);77         }else if(("download").equals(op)){78             su.initialize(getServletConfig(), request, response);//初始化79             su.setContentDisposition(null);80             String filename1 = request.getParameter("filename");81             String filename = new String(filename1.getBytes("ISO-8859-1"), "GB2312");82             System.out.println(filename);83             try {84                 su.downloadFile("/smartUpdownload/"+filename);85             } catch (SmartUploadException e) {86                 e.printStackTrace();87             }88         }89         90         91         92         93     }94 95 }

jsp 內容如下:

這裡使用到了  c 標籤,所以需要兩個jar包  jstl.jar 和   standard-1.1.2.jar

並使用

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 匯入包
 1 <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 3 <%@page import="org.lxh.smart.SmartUpload"%> 4 <%@page import="org.lxh.smart.File"%> 5 <% 6 String path = request.getContextPath(); 7 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 8 %> 9 10 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">11 <html>12   <head>13     <base href="<%=basePath%>">14     15     <title>My JSP ‘fileUpDownload.jsp‘ starting page</title>16     17     <meta http-equiv="pragma" content="no-cache">18     <meta http-equiv="cache-control" content="no-cache">19     <meta http-equiv="expires" content="0">    20     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">21     <meta http-equiv="description" content="This is my page">22     <!--23     <link rel="stylesheet" type="text/css" href="styles.css">24     -->25 26   </head>27   28   <body>29     <form action="FileUpdownLoad?op=upload"  method="post" enctype="multipart/form-data" >30             <input  type="file" name="file1" value="上傳的檔案">31             <input type="submit" value="上傳">32     </form>33     <hr/>34     <a href = "FileUpdownLoad?op=showFiles">顯示下載的檔案目錄</a><br/>35     <c:forEach  items="${list}" var="filename">  36         <a href="FileUpdownLoad?op=download&filename=${filename }">下載檔案:${filename }</a><br/>37     </c:forEach>38     39     <a href="ImageServlet">顯示浮水印圖片</a>40   </body>41 </html>

 

 

java 檔案的上傳和下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.