zoukankan      html  css  js  c++  java
  • java 文件的上传和下载

    主要介绍使用 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>

  • 相关阅读:
    ZOJ 3705 Applications
    UVA 220 Othello
    HDU 2084 数塔
    第五章:变量
    第四章:注释
    第三章:程序结构
    第二章:项目的创建和环境熟悉
    第一章:c#开发环境安装
    处理字段串
    查询表的列信息
  • 原文地址:https://www.cnblogs.com/nn369/p/7642497.html
Copyright © 2011-2022 走看看