JAVA 利用http POST上传文件

import java.io.*;    
import java.net.*;    
public class upload {    
                  
private static byte[] readFile(String file)throws Exception    
{    
FileInputStream   fis   =   new   FileInputStream(file);      
  byte[]   b   =   new   byte[256];      
  int   l;      
  ByteArrayOutputStream   bos   =   new   ByteArrayOutputStream();      
  while((l=fis.read(b))   !=   -1){      
bos.write(b,0,l);      
  }      
  fis.close();      
  return   bos.toByteArray();    
}    
                  
public static void main(String[] args) throws Exception{    
URL url = new URL("http://ustor.cn/newupload/");    
      HttpURLConnection connection=(HttpURLConnection)url.openConnection();    
       //设置请求方式    
         connection.setRequestMethod("POST");    
          connection.setDoOutput(true);    
          //读取文件内容到byte[]    
       //File file=new File("d:\\s.txt");    
       //FileOutputStream fos=new FileOutputStream(file);    
                     
   /*    
   byte buff;    
   StringBuffer file_buff =new StringBuffer();    
   while((buff=fis.read())!=-1)    
   {    
file_buff.append(new String(buff));    
   }    
   */
                  
       //byte[] f="abcdefghijklmnopqrstuvwxyz".getBytes();    
       //byte[] f=readFile("d:\\s.txt");    
       byte[] f=new FileUtil().getFileByte("E:\\My Documents\\Desktop\\对象存储\\对象存储_国内专利分析.jpg");    
   System.out.println("字节数组的大小:"+f.length);    
   /*    
   for(int i=0;i<f.length;i++)    
   {    
System.out.print(f[i]);    
   }    
   */
       //fos.write(f);    
      // 分隔符    
       String BOUNDARY = "---------------------------265001916915724";    
       StringBuffer sb = new StringBuffer();    
                        
       //发送文件:    
       sb = sb.append("--");    
       sb = sb.append(BOUNDARY);    
       sb = sb.append("\r\n");    
       sb = sb.append("Content-Disposition: form-data; name=\"file\"; filename=\"对象存储_国内专利分析.jpg\"\r\n");    
       sb = sb.append("Content-Type: image/jpeg\r\n\r\n");    
       byte[] data = sb.toString().getBytes();    
       byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();    
//        设置HTTP头:    
       connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+ BOUNDARY);    
       connection.setRequestProperty("Content-Length", String.valueOf(data.length + f.length + end_data.length));    
//        输出:    
      OutputStream out=connection.getOutputStream();    
       out.write(data);    
       out.write(f);    
       out.write(end_data);    
       out.flush();    
//        读取服务器响应:    
BufferedReader in=new BufferedReader(new InputStreamReader(connection.getInputStream()));    
String line=null;    
while((line=in.readLine())!=null){    
   System.out.println(line);    
                  
}    
in.close();    
out.close();    
                  
}    
                  
}    
                  
class FileUtil {    
                  
public byte[] getFileByte(String fileName) throws FileNotFoundException {    
  FileInputStream fileInputStream = new FileInputStream(fileName);    
  return getFileByte(fileInputStream);    
}    
                  
public byte[] getFileByte(URL url) throws IOException {    
  if (url != null) {    
   return getFileByte(url.openStream());    
  } else {    
   return null;    
  }    
}    
                  
public byte[] getFileByte(InputStream in) {    
  ByteArrayOutputStream out = new ByteArrayOutputStream(4096);    
  try {    
   copy(in, out);    
  } catch (IOException e) {    
   e.printStackTrace();    
  }    
  return out.toByteArray();    
                  
}    
                  
private void copy(InputStream in, OutputStream out) throws IOException {    
                  
  try {    
   byte[] buffer = new byte[4096];    
   int nrOfBytes = -1;    
   while ((nrOfBytes = in.read(buffer)) != -1) {    
    out.write(buffer, 0, nrOfBytes);    
   }    
   out.flush();    
  } catch (IOException e) {    
                  
  }finally {    
   try {    
    if (in != null) {    
     in.close();    
    }    
   } catch (IOException ex) {    
   }    
   try {    
    if (out != null) {    
     out.close();    
    }    
   } catch (IOException ex) {    
   }    
  }    
                  
}    
                  
}

 可结合FireFox的插件:HttpFox和 Live HTTP header 来设置请求的头部

你可能感兴趣的:(JAVA;WEB)