Java资源网

| JAVA基础 | 环境配置 | JDBC | 线程技术 | Socket编程 | JavaMail | JAVA与XML | 设计模式 | 技术新闻 | Java认证 | 程序人生 软件下载
| JSP&Servlet | Spring | Struts | Hibernate | JBuilder | Eclipse | WebService | EJB技术 | J2ME开发 | 应用服务器 | JXTA | Ajax
Articles search文章搜索
   关键字:
   类 别:
       
New download 最新下载
· [组件]HTML Parser 1.5
· [教程]WebSphere Studio应用教程
· [组件]JDom 1.0
· [工具]Junit3.8.1
· [教程]EJB编程及J2EE系统架构和设计
· [教程]EJB教程
· [教程]J2EE Tutorial中文版
· [教程]Java编程思想2(英文)
· [教程]java编程思想(完整版)
· [教程]Java网络编程
New articles 最新文章
· 设计移动 Web 服务
· 解析XML的时候完全忽略DTD
· 理解XML Schema XML Schema 初步
· 标签库的深入研究
· 提升JSP应用程序的七大绝招
· 如何使用JDOM对XML文件进行操作
· 处理XML字符串中特殊字符
· 利用Digester把XML转换成为Java对象
· 使用WebService 和RMI远程协作
· 使用Axis开发Web Service程序
Articles top 热门文章
· Eclipse基础--plugin插件安装(6644)
· eclipse+tomcat+lomboz的安装配置说明(4774)
· Java程序员就业前景(4584)
· Windows下JAVA环境变量的设置祥解(3788)
· Tomcat下JSP、Servlet和JavaBean环境的配置(3716)
· 使用links方式安装Eclipse插件(3698)
· 一个老程序员的心理话(3533)
· linux下jdk的安装与配置(3459)
· 初学者入门:Structs中基本配置入门(3334)
· Eclipse 运行命令行参数大全(3084)
您的位置:首页>>JSP和Servlet>>使用jsp编写文件上传程序
使用jsp编写文件上传程序
2005-08-18   来源:Java资源网  作者:Java资源网
public class UploadServlet extends HttpServlet
{
 //default maximum allowable file size is 100k
 static final int MAX_SIZE = 102400;
 //instance variables to store root and success message
 String rootPath, successMessage;
 /**
  * init method is called when servlet is initialized.
  */
 public void init(ServletConfig config) throws ServletException
 {
  super.init(config);
  //get path in which to save file
  rootPath = config.getInitParameter("RootPath");
  if (rootPath == null)
  {
   rootPath = "/";
  }
  /*Get message to show when upload is complete. Used only if
   a success redirect page is not supplied.*/
  successMessage = config.getInitParameter("SuccessMessage");
  if (successMessage == null)
  {
   successMessage = "File upload complete!";
  }
 }
 /**
  * doPost reads the uploaded data from the request and writes
  * it to a file.
  */
 public void doPost(HttpServletRequest request,
  HttpServletResponse response)
 {
  ServletOutputStream out=null;
  DataInputStream in=null;
  FileOutputStream fileOut=null;
  try
  {
   /*set content type of response and get handle to output
    stream in case we are unable to redirect client*/
   response.setContentType("text/plain");
   out = response.getOutputStream();
  }
  catch (IOException e)
  {
   //print error message to standard out
   System.out.println("Error getting output stream.");
   System.out.println("Error description: " + e);
   return;
  }
  try
  {
   //get content type of client request
   String contentType = request.getContentType();
   //make sure content type is multipart/form-data
   if(contentType != null && contentType.indexOf(
    "multipart/form-data") != -1)
   {
    //open input stream from client to capture upload file
    in = new DataInputStream(request.getInputStream());
    //get length of content data
    int formDataLength = request.getContentLength();
    //allocate a byte array to store content data
    byte dataBytes[] = new byte[formDataLength];
    //read file into byte array
    int bytesRead = 0;
    int totalBytesRead = 0;
    int sizeCheck = 0;
    while (totalBytesRead < formDataLength)
    {
     //check for maximum file size violation
     sizeCheck = totalBytesRead + in.available();
     if (sizeCheck > MAX_SIZE)
     {
      out.println("Sorry, file is too large to upload.");
      return;
     }
     bytesRead = in.read(dataBytes, totalBytesRead,
      formDataLength);
     totalBytesRead += bytesRead;
    }
    //create string from byte array for easy manipulation
    String file = new String(dataBytes);
    //since byte array is stored in string, release memory
    dataBytes = null;
    /*get boundary value (boundary is a unique string that
     separates content data)*/
    int lastIndex = contentType.lastIndexOf("=");
    String boundary = contentType.substring(lastIndex+1,
     contentType.length());
    //get Directory web variable from request
    String directory="";
    if (file.indexOf("name=\"Directory\"") > 0)
    {
     directory = file.substring(
      file.indexOf("name=\"Directory\""));
     //remove carriage return
     directory = directory.substring(
      directory.indexOf("\n")+1);
     //remove carriage return
     directory = directory.substring(
      directory.indexOf("\n")+1);
     //get Directory
     directory = directory.substring(0,
      directory.indexOf("\n")-1);
     /*make sure user didn´t select a directory higher in
      the directory tree*/
     if (directory.indexOf("..") > 0)
     {
      out.println("Security Error: You can´t upload " +
       "to a directory higher in the directory tree.");
      return;
     }
    }
    //get SuccessPage web variable from request
    String successPage="";
    if (file.indexOf("name=\"SuccessPage\"") > 0)
    {
     successPage = file.substring(
      file.indexOf("name=\"SuccessPage\""));
     //remove carriage return
     successPage = successPage.substring(
      successPage.indexOf("\n")+1);
     //remove carriage return
     successPage = successPage.substring(
      successPage.indexOf("\n")+1);
     //get success page
     successPage = successPage.substring(0,
      successPage.indexOf("\n")-1);
    }
    //get OverWrite flag web variable from request
    String overWrite;
    if (file.indexOf("name=\"OverWrite\"") > 0)
    {
     overWrite = file.substring(
      file.indexOf("name=\"OverWrite\""));
     //remove carriage return
     overWrite = overWrite.substring(
      overWrite.indexOf("\n")+1);
     //remove carriage return
     overWrite = overWrite.substring(
      overWrite.indexOf("\n")+1);
     //get overwrite flag
     overWrite = overWrite.substring(0,
      overWrite.indexOf("\n")-1);
    }
    else
    {
     overWrite = "false";
    }
    //get OverWritePage web variable from request
    String overWritePage="";
    if (file.indexOf("name=\"OverWritePage\"") > 0)
    {
     overWritePage = file.substring(
      file.indexOf("name=\"OverWritePage\""));
     //remove carriage return
     overWritePage = overWritePage.substring(
      overWritePage.indexOf("\n")+1);
     //remove carriage return
     overWritePage = overWritePage.substring(
      overWritePage.indexOf("\n")+1);
     //get overwrite page
     overWritePage = overWritePage.substring(0,
      overWritePage.indexOf("\n")-1);
    }
    //get filename of upload file
    String saveFile = file.substring(
     file.indexOf("filename=\"")+10);
    saveFile = saveFile.substring(0,
     saveFile.indexOf("\n"));
    saveFile = saveFile.substring(
     saveFile.lastIndexOf("\\")+1,
     saveFile.indexOf("\""));
    /*remove boundary markers and other multipart/form-data
     tags from beginning of upload file section*/
    int pos; //position in upload file
    //find position of upload file section of request
    pos = file.indexOf("filename=\"");
    //find position of content-disposition line
    pos = file.indexOf("\n",pos)+1;
    //find position of content-type line
    pos = file.indexOf("\n",pos)+1;
    //find position of blank line
    pos = file.indexOf("\n",pos)+1;
    /*find the location of the next boundary marker
     (marking the end of the upload file data)*/
    int boundaryLocation = file.indexOf(boundary,pos)-4;
    //upload file lies between pos and boundaryLocation
    file = file.substring(pos,boundaryLocation);
    //build the full path of the upload file
    String fileName = new String(rootPath + directory +
     saveFile);
    //create File object to check for existence of file
    File checkFile = new File(fileName);
    if (checkFile.exists())
    {
     /*file exists, if OverWrite flag is off, give
      message and abort*/
     if (!overWrite.toLowerCase().equals("true"))
     {
      if (overWritePage.equals(""))
      {
       /*OverWrite HTML page URL not received, respond
        with generic message*/
       out.println("Sorry, file already exists.");
      }
      else
      {
       //redirect client to OverWrite HTML page
       response.sendRedirect(overWritePage);
      }
      return;
     }
    }
    /*create File object to check for existence of
     Directory*/
    File fileDir = new File(rootPath + directory);
    if (!fileDir.exists())
    {
     //Directory doesn´t exist, create it
     fileDir.mkdirs();
    }
    //instantiate file output stream
    fileOut = new FileOutputStream(fileName);
    //write the string to the file as a byte array
    fileOut.write(file.getBytes(),0,file.length());
    if (successPage.equals(""))
    {
     /*success HTML page URL not received, respond with
      generic success message*/
     out.println(successMessage);
     out.println("File written to: " + fileName);
    }
    else
    {
     //redirect client to success HTML page
     response.sendRedirect(successPage);
    }
   }
   else //request is not multipart/form-data
   {
    //send error message to client
    out.println("Request not multipart/form-data.");
   }
  }
  catch(Exception e)
  {
   try
   {
    //print error message to standard out
    System.out.println("Error in doPost: " + e);
    //send error message to client
    out.println("An unexpected error has occurred.");
    out.println("Error description: " + e);
   }
   catch (Exception f) {}
  }
  finally
  {
   try
   {
    fileOut.close(); //close file output stream
   }
   catch (Exception f) {}
   try
   {
    in.close(); //close input stream from client
   }
   catch (Exception f) {}
   try
   {
    out.close(); //close output stream to client
   }
   catch (Exception f) {}
  }
 }
}
  --相关文章--
· 漫步j2ee之jsp技术(1) (2007-04-13)
· 提升JSP应用程序的七大绝招 (2007-04-13)
· 如何在JSP中处理中文 (2007-04-13)
· 第一章 taglibnbsp;原理和实现 (2007-04-13)
· 用jsp动态输出excel文档和中文乱码问题的解决 (2007-04-13)
· 可以自动跳转到出错页面的servlet jsp框架 (2007-04-13)

版权所有©2005-2006 JAVA资源网 渝ICP备05007591号 虚拟主机 | 关于我们 | 联系方式 | 广告业务 | 网站地图 | 友情链接