`
roger51
  • 浏览: 26484 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

java c++ and c# fileupload

    博客分类:
  • java
阅读更多

最近在作文件上传,小文件到无所谓,可是一般情况下都是大文件所以经常会出outofmemory,自己写了个小程序,目前可以多线程上传,也可以通过c++客户端和c#客户端上传,附件里的程序是c++和c#的,大概想法就是把大文件切分成小文件每次上传2M直到文件结束,也可以扩展下支持断点续传

import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.RequestContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import javax.servlet.ServletInputStream;
import java.io.*;
import java.util.*;
import java.net.URLDecoder;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;


/**
 * 服务器端
 */
public class UploadCFileServlet extends HttpServlet  {

      private Map map = null;
      public void init() throws ServletException
      {
          super.init();
          try {
              map = new HashMap();
          } catch (Exception e) {
              e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
          }
      }
      public void destroy()
      {
         super.destroy();
          try {
              if(map != null)
              {
                  map.clear();
              }
          } catch (Exception e) {
              e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
          }
      }
      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      {

          String reqSessionId = request.getHeader("Cookie");
//          System.out.println("reqSessionId = "+reqSessionId);
          String FileEnd = request.getHeader("FileEnd");
//          System.out.println("FileEnd = "+FileEnd);
          //true  uploadfile is end ,false go on upload
          boolean isEnd = false;

          response.setContentType("text/html");
          PrintWriter out = response.getWriter();
          File newFile = null;
          try {
              String path = this.getServletContext().getRealPath("/");
              request.setCharacterEncoding("UTF-8");
              RequestContext requestContext = new ServletRequestContext(request);
              if (FileUpload.isMultipartContent(requestContext)) {
                  DiskFileItemFactory factory = new DiskFileItemFactory();
                  factory.setRepository(new File(path));
//                  factory.setSizeThreshold(8096);
                  ServletFileUpload upload = new ServletFileUpload(factory);
                  upload.setSizeMax(100*1024*1024);
                  List items = new ArrayList();
                  try {
                      items = upload.parseRequest(request);
                  } catch (FileUploadException e1) {
                      System.out.println("文件上传发生错误" + e1.getMessage());
                  }

                  Iterator it = items.iterator();
                  while (it.hasNext()) {
                      FileItem fileItem = (FileItem) it.next();
                      if (!fileItem.isFormField()) {
                          String fileName = URLDecoder.decode(fileItem.getName(), "utf-8");
                          System.out.println(fileItem.getFieldName() + "  " + fileName  + "  " +fileItem.isInMemory() + "   " +fileItem.getContentType() + "   " +fileItem.getSize());
                          if (fileItem.getName() != null && fileItem.getSize() != 0) {
                              File fullFile = new File(fileName);
                              newFile = new File(path+"/sharefolder/" + fullFile.getName());
                              // if the file exists and the sessionid is also exists so add the file
                              if(reqSessionId !=null &&isExists(reqSessionId))
                              {

                                  InputStream is = fileItem.getInputStream();
                                  int length = is.available();
                                  System.out.println("filitem length ="+length);
                                  byte[] buffer = new byte[length];
                                  is.read(buffer,0,length);
                                  RandomAccessFile raf = new RandomAccessFile(newFile, "rw");
                                  long fileLength = Long.valueOf(newFile.length()).longValue();
                                  System.out.println("fileLength ="+fileLength);
                                  raf.seek(fileLength);
                                  raf.write(buffer);
                                  raf.close();
                                  is.close();
                                  // if true uploadfile is end ,false go on upload
                                  isEnd = Boolean.valueOf(FileEnd).booleanValue();
                                  if(isEnd)
                                  {
                                      removeSessionId(reqSessionId);
                                  }

                              }
                              // a new file is upload
                              else
                              {
                                  try {
                                      fileItem.write(newFile);
                                      // if true uploadfile is end ,false go on upload
                                      isEnd = Boolean.valueOf(FileEnd).booleanValue();
                                  } catch (Exception e) {
                                      e.printStackTrace();
                                  }
                              }
                          }
                      }

                  }
              }
              out.println("200");

              if(!isEnd)
              {
                  if(reqSessionId == null)
                  {
                      HttpSession hs = request.getSession();
                      reqSessionId = hs.getId();
                  }
                  map.put(reqSessionId,newFile);
                  response.addHeader("Cookie",reqSessionId);
              }
          } catch (IOException e) {
              out.println("400");
              e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
          } finally {
          }

      }
      public boolean isExists(String sessionId)
      {
          if(map.get(sessionId) != null)
          {
              return true;
          }
          return false;
      }
      public void removeSessionId(String sessionId)
      {
          System.out.println("remove sessionId="+sessionId);
         map.remove(sessionId);
          System.out.println("map size="+map.size());

      }
          public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      {
          this.doPost(request,response);
      }

 

}

//客户端
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Set;

public class testupload {

 public static void main(String[] args) throws Exception {
  HttpURLConnection conn = null;
  BufferedReader br = null;
  DataOutputStream dos = null;
  DataInputStream inStream = null;

  InputStream is = null;
  OutputStream os = null;
  boolean ret = false;
  String StrMessage = "";
  String exsistingFileName = "d:\\jdk150.ZH_cn.chm";
  //String exsistingFileName ="e:\\[IBM.Lotus.Domino-Notes.7.0.1简体中文版全集].Lotus_Domino_Server_7.0.1_zh-CN_Windows.zip";
  String lineEnd = "\r\n";
  String twoHyphens = "--";
  String boundary = "*****";
  int bytesRead, bytesAvailable, bufferSize, start;
  byte[] buffer;
  int maxBufferSize = 1 * 1024 * 1024;
  String responseFromServer = "";
  String urlString = "http://192.168.1.12/UploadCFileServlet";
  String Cookie = null;
  String FileEnd = null;
  System.out.println("=====================================");
  try {
   //------------------ CLIENT REQUEST
   long start1 = System.currentTimeMillis();
   long end1 = 0;
   FileInputStream fileInputStream = new FileInputStream(new File(
     exsistingFileName));

   bytesAvailable = fileInputStream.available();
   System.out.println("bytesAvailable ===" + bytesAvailable);
   bufferSize = 2 * 1024 * 1024;
   buffer = new byte[bufferSize];
   // read file and write it into form...
   start = 0;
   // bytesRead = fileInputStream.read(buffer, 0, bufferSize);
   int i = 0;
   boolean flag = true;
   while (flag) {

    if (bytesAvailable > 0) {
     int newAvailable = fileInputStream.available();
     if (newAvailable < bufferSize) {
      bufferSize = newAvailable;
      buffer = new byte[bufferSize];
      flag = false;
      FileEnd = "true";

     }
     System.out.println("new available = " + newAvailable);
     bytesAvailable = fileInputStream
       .read(buffer, 0, bufferSize);
     System.out.println("bytesRead ===" + bytesAvailable);
    }

    // open a URL connection to the Servlet
    URL url = new URL(urlString);
    // Open a HTTP connection to the URL
    conn = (HttpURLConnection) url.openConnection();
    // Allow Inputs
    conn.setDoInput(true);
    // Allow Outputs
    conn.setDoOutput(true);
    // Don't use a cached copy.
    conn.setUseCaches(false);
    // Use a post method.
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Connection", "Keep-Alive");

    conn.setRequestProperty("Content-Type",
      "multipart/form-data;boundary=" + boundary);
    if (Cookie != null)
     conn.setRequestProperty("Cookie", Cookie);
    if (FileEnd != null)
     conn.setRequestProperty("FileEnd", FileEnd);
    dos = new DataOutputStream(conn.getOutputStream());
    dos.writeBytes(twoHyphens + boundary + lineEnd);
    dos
      .writeBytes("Content-Disposition: form-data; name=\"uploadfile\";"
        + " filename=\""
        + URLEncoder.encode(exsistingFileName, "utf-8")
        + "\"" + lineEnd);
    dos.writeBytes(lineEnd);
    //          String str1 = new String(buffer);
    //          System.out.println("str1 ="+str1);
    int length = buffer.length;
    System.out.println("length =" + length);
    dos.write(buffer, 0, bufferSize);

    // send multipart form data necesssary after file data...
    dos.writeBytes(lineEnd);
    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
    // close streams

    dos.flush();
    dos.close();

    //------------------ read the SERVER RESPONSE

    try {
     inStream = new DataInputStream(conn.getInputStream());
     Map headers = conn.getHeaderFields();
     Set keys = headers.keySet();
     for (String key : keys) {
      String val = conn.getHeaderField(key);
      System.out.println(key + "    " + val);
      if (key != null && key.equalsIgnoreCase("Cookie")) {
       Cookie = val;
      }
     }
     String str;

     while ((str = inStream.readLine()) != null) {
      System.out.println("Server response is: " + str);
      System.out.println("");
     }
     inStream.close();
    } catch (IOException ioex) {
     System.out.println("From (ServerResponse): " + ioex);
    }

    System.out.println("=====================================");
   }
   fileInputStream.close();
   end1 = System.currentTimeMillis();
   System.out.println("upload times" + ((end1 - start1) / 1000)
     + " seconds.");
  } catch (MalformedURLException ex) {
   System.out.println("From ServletCom CLIENT REQUEST:" + ex);
  } catch (IOException ioe) {
   System.out.println("From ServletCom CLIENT REQUEST:" + ioe);
  }
 }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics