zoukankan      html  css  js  c++  java
  • cos-26上传

    在开发中常常需要上传文件,上传文件的方式有很多种,这里有一个cos实现的例子。

    首先是要拷贝cos.jar包拷贝到WEB-INF/lib目录下,然后才进行编码。

    创建一个可以进行自动重命名的Java文件FileRenameUtil.java:

     
    1. package org.ml.drp.util;  
    2.   
    3. import java.io.File;  
    4. import java.util.Date;  
    5.   
    6. import com.oreilly.servlet.multipart.FileRenamePolicy;  
    7.   
    8. /** 
    9.  * 文件重命名 
    10.  * @author MuLing 
    11.  * 
    12.  */  
    13. public class FileRenameUtil implements FileRenamePolicy {  
    14.   
    15.     public File rename(File file) {  
    16.         String body = "";  
    17.         String ext = "";  
    18.         Date date = new Date();  
    19.         int pot = file.getName().lastIndexOf(".");//取得文件名和后缀分割点  
    20.         if (pot != -1) {//说明后缀存在  
    21.             body = date.getTime() + "";//采用时间的形式命名  
    22.             ext = file.getName().substring(pot);//截取后缀名  
    23.         } else {  
    24.             body = (new Date()).getTime() + "";  
    25.             ext = "";  
    26.         }  
    27.         String newName = body + ext;  
    28.         file = new File(file.getParent(), newName);//对文件进行重命名  
    29.         return file;  
    30.   
    31.     }  
    32.   
    33. }  

    然后创建一个实现上传功能的servlet,为了方便查看上传情况,所以加入了一些输出语句。

    FileUploadServlet.java

     
    1. package org.ml.servlet;  
    2.   
    3. import java.io.File;  
    4. import java.io.IOException;  
    5. import java.util.Enumeration;  
    6.   
    7. import javax.servlet.ServletException;  
    8. import javax.servlet.http.HttpServlet;  
    9. import javax.servlet.http.HttpServletRequest;  
    10. import javax.servlet.http.HttpServletResponse;  
    11.   
    12. import com.oreilly.servlet.MultipartRequest;  
    13.   
    14. /** 
    15.  * 能够进行文件上传的servlet类 
    16.  * @author MuLing 
    17.  * 
    18.  */  
    19. public class UploadServlet extends HttpServlet {   
    20.     private static final long serialVersionUID = 1L;  
    21.   
    22.     public UploadServlet() {  
    23.         super();  
    24.     }  
    25.    
    26.     public void destroy() {  
    27.         super.destroy();    
    28.     }  
    29.    
    30.     @SuppressWarnings("unchecked")  
    31.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
    32.             throws ServletException, IOException {  
    33.         // 存绝对路径  
    34.         // String filePath = "C://upload";  
    35.         // 存相对路径  
    36.         String filePath = getServletContext().getRealPath("/") + "upload";  
    37.         System.out.println(filePath);//输出存放上传文件所到的路径  
    38.         File uploadPath = new File(filePath);  
    39.         // 检查文件夹是否存在 不存在 创建一个  
    40.         if (!uploadPath.exists()) {  
    41.             uploadPath.mkdir();  
    42.         }  
    43.         // 文件最大容量 3M  
    44.         int fileMaxSize = 3 * 1024 * 1024;  
    45.         // 存放文件描述  
    46.         @SuppressWarnings("unused")  
    47.         String[] fileDiscription = { null, null };  
    48.         // 文件名  
    49.         String fileName = null;  
    50.         // 上传文件数  
    51.         int fileCount = 0;  
    52.         // 重命名策略  
    53.         RandomFileRenamePolicy rfrp = new RandomFileRenamePolicy();  
    54.         // 上传文件  
    55.         MultipartRequest mulit = new MultipartRequest(request, filePath,  
    56.                 fileMaxSize, "UTF-8", rfrp);//取得上传文件  
    57.   
    58.         String userName = mulit.getParameter("userName");  
    59.         System.out.println(userName);  
    60.   
    61.         Enumeration filesname = mulit.getFileNames();//取得上传的所有文件(相当于标识)   
    62.         while (filesname.hasMoreElements()) {  
    63.             String name = (String) filesname.nextElement();//标识  
    64.             System.out.println(name);  
    65.             fileName = mulit.getFilesystemName(name); //取得文件名  
    66.             String contentType = mulit.getContentType(name);//工具标识取得的文件类型  
    67.             if (fileName != null) {  
    68.                 fileCount++;  
    69.             }  
    70.             System.out.println("文件名:" + fileName);  
    71.             System.out.println("文件类型: " + contentType);   
    72.         }  
    73.         System.out.println("共上传" + fileCount + "个文件!");   
    74.     }  
    75.    
    76.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
    77.             throws ServletException, IOException {   
    78.         this.doGet(request, response);  
    79.     }   
    80.       
    81.     public void init() throws ServletException {  
    82.     }  
    83.   
    84. }  


    在web.xml文件中的配置servlet如下:

     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app version="2.5"   
    3.     xmlns="http://java.sun.com/xml/ns/javaee"   
    4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
    7.   <servlet>   
    8.     <servlet-name>UploadServlet</servlet-name>  
    9.     <servlet-class>org.ml.servlet.UploadServlet</servlet-class>  
    10.   </servlet>  
    11.   
    12.   <servlet-mapping>  
    13.     <servlet-name>UploadServlet</servlet-name>  
    14.     <url-pattern>/UploadServlet</url-pattern>  
    15.   </servlet-mapping>  
    16.     
    17.   <welcome-file-list>  
    18.     <welcome-file>index.jsp</welcome-file>  
    19.   </welcome-file-list>  
    20. </web-app>  


    上传页面代码如下:

     
    1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
    2. <%  
    3. String path = request.getContextPath();  
    4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
    5. %>  
    6.   
    7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
    8. <html>  
    9.   <head>   
    10.   </head>  
    11.     
    12.   <body>  
    13.     <form enctype="multipart/form-data" method = "post" action = "UploadServlet">  
    14.         <input type="text" name="userName" />   
    15.        <p>上传文件1:<input type = "file" name = "File1" size = "20" maxlength = "20"><br></p>  
    16.        <p>上传文件2:<input type = "file" name = "File2" size = "20" maxlength = "20"><br></p>  
    17.        <p>上传文件3:<input type = "file" name = "File3" size = "20" maxlength = "20"><br></p>  
    18.        <p>上传文件4:<input type = "file" name = "File4" size = "20" maxlength = "20"><br></p>  
    19.        <input type = "submit" value = "上传">  
    20.     </form>  
    21.   </body>  
    22. </html>  


    上面完成同时上传一个或者多个文件的操作,使用起来很方便。

  • 相关阅读:
    Ubuntu adb devices :???????????? no permissions (verify udev rules) 解决方法
    ubuntu 关闭显示器的命令
    ubuntu android studio kvm
    ubuntu 14.04版本更改文件夹背景色为草绿色
    ubuntu 创建桌面快捷方式
    Ubuntu 如何更改用户密码
    ubuntu 14.04 返回到经典桌面方法
    ubuntu 信使(iptux) 创建桌面快捷方式
    Eclipse failed to get the required ADT version number from the sdk
    Eclipse '<>' operator is not allowed for source level below 1.7
  • 原文地址:https://www.cnblogs.com/ly-china/p/5427441.html
Copyright © 2011-2022 走看看