« java类加载原理分析(转载)谈一下文件上传 »

Servlet接收二进制

/*  
    *   UpLoadServlet.java  
    */  
  package   com.jetmail.Tool;  
   
  import   javax.servlet.*;  
  import   javax.servlet.http.*;  
  import   java.io.*;  
  import   com.jetmail.Tool.*;  
   
  /*  
    *   文件上传下载  
    *   @author   cxj  
    *   @version   1.0.0  
    *   @date   2002/08/23  
    */  
   
  public   class   UpLoadServlet   extends   HttpServlet   {  
  private   final   int   BUFFERSIZE   =   1024   *   8;  
  private   final   String   splitLine   =   File.separator;  
  private   String   tempAttachmentFileName;  
   
  protected   void   doPost(HttpServletRequest   request,   HttpServletResponse   response)   throws   ServletException,   IOException   {  
  //得到附件的路径  
  HttpSession   session   =   request.getSession(false);  
  com.jetmail.User.Session   mailSession   =   (com.jetmail.User.Session)session.getAttribute("mailSession");  
  com.jetmail.Tool.Config   mailConfig   =   com.jetmail.Tool.Config.getInstance();  
  //计算能够上传附件的大小  
  int   maxLen   =   mailConfig.attachMentSize   *   1024   *   1024;  
  String   attachmentPath   =   mailConfig.tempFolder   +   splitLine;  
  attachmentPath   =   attachmentPath   +   mailSession.getDomain()   +   splitLine;  
  attachmentPath   =   attachmentPath   +   mailSession.getUserName()   +   splitLine;  
  attachmentPath   =   attachmentPath   +   mailConfig.attachmentFolder   +   splitLine;  
  tempAttachmentFileName   =   mailSession.getDomain()   +   '-'   +   mailSession.getUserName();  
  //得到附件  
  ServletOutputStream   out   =   response.getOutputStream();  
  int   contentLen   =   request.getContentLength();  
  if(contentLen   <   maxLen)   {  
  String   contentType   =   request.getContentType();  
  String   boundary   =   getBoundary(contentType);  
  ServletInputStream   in   =   request.getInputStream();  
  FileOutputStream   fou   =   null;  
  byte[]   b   =   new   byte[BUFFERSIZE];  
  int   result;  
  try   {  
  result   =   in.readLine(b,0,b.length);   //读取boundary  
  result   =   in.readLine(b,0,b.length);   //读取Content-Disposition  
  String   upLoadFileName   =   getUpLoadFileName(new   String(b,0,result));  
  fou   =   new   FileOutputStream(attachmentPath   +   tempAttachmentFileName);  
  result   =   in.readLine(b,0,b.length);   //读取Content-Type;  
  result   =   in.readLine(b,0,b.length);   //读取空行;  
  int   totalRead   =   0;  
  result   =   in.readLine(b,0,b.length);  
  while((new   String(b,0,result)).trim().indexOf(boundary)   ==   -1)   {  
  totalRead   +=   result;  
  fou.write(b,0,result);  
  result   =   in.readLine(b,0,b.length);  
  }  
  out.println(totalRead);  
  fou.close();  
  in.close();  
  //处理文件  
  dealFile(upLoadFileName,totalRead,attachmentPath);  
  }   catch(Exception   ex)   {  
  System.out.print(ex.toString());  
  }  
  }  
  response.sendRedirect("/jsp/upload.jsp");  
  }  
   
  public   void   destroy(){}  
   
  /*  
    *   得到filename  
    */  
  private   String   getUpLoadFileName(String   line)   {  
  int   split   =   line.indexOf("filename=");  
  String   tempFileName   =   delQuote(line.substring(split   +   9,line.length()).trim());  
  if(tempFileName.indexOf("\\")   !=   -1)   {  
  tempFileName   =   tempFileName.substring(tempFileName.lastIndexOf("\\")   +   1,tempFileName.length());  
  }  
  return   tempFileName;  
  }  
   
  /*  
    *   得到分隔符  
    */  
  private   String   getBoundary(String   line)   {  
  return   line.substring(line.indexOf("boundary=")   +   9,line.length()).trim();  
  }  
   
  /*  
    *   去除""  
    */  
  private   String   delQuote(String   line)   {  
  if(line.indexOf("\"")   !=   -1)   {  
  line   =   line.substring(1,(line.length()   -   1));  
  }  
  return   line;  
  }  
   
  /*  
    *   处理文件,把最后的两个字节删除  
    */  
  private   void   dealFile(String   fileName,   int   totalRead,   String   attachmentPath)   throws   IOException   {  
  File   file   =   new   File(attachmentPath   +   tempAttachmentFileName);  
  byte[]   b   =   new   byte[totalRead   -   2];  
  RandomAccessFile   rafRead   =   new   RandomAccessFile(file,"r");  
  rafRead.readFully(b,0,totalRead   -   2);  
  rafRead.close();  
  file.delete();  
  RandomAccessFile   rafWrite   =   new   RandomAccessFile(attachmentPath   +   fileName,"rw");  
  rafWrite.write(b);  
  rafWrite.close();  
  }  
   
  }  
  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Powered By Z-Blog 1.8 Arwen Build 81206

湘 ICP 备 06003756 号
Copyright 山寨 Rights Reserved.