import java.io.BufferedReader;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;@SuppressWarnings("unused")
public class UploadFile {

protected static String sessionKey;
protected static String sha1;
protected static String fileData;
protected static String appkey; @SuppressWarnings("static-access")
public UploadFile(String sessionKey,String sha1,
String fileData){
this.sessionKey=sessionKey;
this.sha1=sha1;
this.fileData=fileData;

}
     public static String upFile(){
            appkey="11111111111111111111111";
      File file=new File(fileData);
      
         TreeMap<String, String> apiparamsMap = new TreeMap<String, String>();
         
         apiparamsMap.put("Method", "user_upload");
         apiparamsMap.put("SessionKey", sessionKey);
         apiparamsMap.put("Sha1",sha1);
         apiparamsMap.put("Filedata_name","1.jar");
         apiparamsMap.put("Filedata",fileData);
         apiparamsMap.put("Appkey",appkey);                
         apiparamsMap.put("StartNumber","1");
         
         StringBuilder param = new StringBuilder();
         for (Iterator<Map.Entry<String, String>> it = apiparamsMap.entrySet()
         .iterator(); it.hasNext();) {
             Map.Entry<String, String> e = it.next();
             param.append("&").append(e.getKey()).append("=").append(e.getValue());
         }
         //System.out.println(param.toString().substring(1));
         return param.toString().substring(1);
         
     }
     
     
     
     public static String getResult(String urlStr, String content) {
         URL url = null;
         HttpURLConnection connection = null;
         int bytesRead, bytesAvailable, bufferSize;
         byte[] bufferFile;
         int maxBufferSize = 2 * 1024 * 1024;
         DataOutputStream dos = null;         
         
         
         try {
                 url = new URL(urlStr);
                 //System.out.println("fileData :\n"+fileData);
                 
                 FileInputStream fileInputStream = new FileInputStream(new File(fileData));
                                                                                    
                 connection =  (HttpURLConnection) url.openConnection();
                 connection.setDoOutput(true);
                 connection.setDoInput(true);
                 connection.setRequestMethod("POST");
                 connection.setRequestProperty("Content-Type:",
                      "multipart/form-data");
                                                
                 connection.setUseCaches(false);                 dos = new DataOutputStream(connection.getOutputStream());
                                                  
                 connection.connect();                             
                 dos.write(content.getBytes("utf-8")); 
                
                 bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);               
                
                bufferFile = new byte[bufferSize];
                //System.out.println("bufferSize\n"+bufferSize);
                
                bytesRead = fileInputStream.read(bufferFile, 0, bufferSize);
              
              
                
                
                                                                                            
               FileOutputStream fos=new FileOutputStream("D:\\a.cod");
             while (bytesRead > 0) {
               
             dos.write(bufferFile, 0, bufferSize);
                    //输出到本地看是不是能全部把读入的文件输出到本地
             fos.write(bufferFile, 0, bufferSize);
             
          
             bytesAvailable = fileInputStream.available();
             bufferSize = Math.min(bytesAvailable, maxBufferSize);
             bytesRead = fileInputStream.read(bufferFile, 0, bufferSize);
            
            }
                 
                 
              fileInputStream.close();
 
                 dos.flush();
                 dos.close();
                 
                 
                 BufferedReader reader =
                 new BufferedReader(new InputStreamReader(connection.getInputStream()));
                 StringBuffer buffer = new StringBuffer();
                 String line = "";
                 while ((line = reader.readLine()) != null) {
                   System.out.println("upfile\n"+line);
                         buffer.append(line);
                 }
                 reader.close();
                 return buffer.toString();
         } catch (IOException e) {
                e.printStackTrace();
         } finally {
                if (connection != null) {
                         connection.disconnect();
                 }
         }
         return null;
 }}