前一段时间,遇到一个需求,需要用java代码了模拟一个http·请求,要可以传递post参数和上传文件。做了些google后,整理如下:
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class HttpRequestMaker{
public HttpRequestMaker( String url ) throws Exception {
m_Url = new URL( url );
}
public final static String boundary = "---------------------------7d71b526e00e4";
public final static String prefix = "--";
public final static String newLine = "\r\n";
public final static String fileContentType = "Content-Type: application/octet-stream\r\n\r\n";
public final static String fileContentDisposition = "Content-Disposition: form-data; name=\"%name\"; filename=\"%fileName\"\r\n";
public final static String paramContentDisposition = "Content-Disposition: form-data; name=\"%name\"\r\n\r\n";
private URL m_Url = null;
private URLConnection uc = null;
private Map m_fileMap = new HashMap();
private Map m_paraMap = new HashMap();
long m_FileTotalSize = 0;
public void AddFile(String name, String filename){
File file = new File( filename );
if( file.exists() ){
m_FileTotalSize += file.length();
m_fileMap.put(name, file);
}
}
public void AddFile(String name, File file){
if( file.exists() ){
m_FileTotalSize += file.length();
m_fileMap.put(name, file);
}
}
public void AddParam( String name, String value){
m_FileTotalSize += value.length();
m_paraMap.put(name, value);
}
public void Upload() throws Exception {
try{
uc = m_Url.openConnection();
uc.setDoOutput( true );
uc.setRequestProperty( "Accept", "*/*" );
uc.setRequestProperty( "Accept-Language", "ja" );
uc.setRequestProperty( "Content-Type", "multipart/form-data; boundary=" + boundary);
uc.setRequestProperty( "Accept-Encoding", "gzip, deflate" );
uc.setRequestProperty( "Connection", "Keep-Alive" );
uc.setRequestProperty( "Cache-Control", "no-cache" );
DataOutputStream dos = new DataOutputStream( uc.getOutputStream() );
for(Iterator iterator = m_fileMap.keySet().iterator(); iterator.hasNext();){
String key = (String)iterator.next();
File file = (File)m_fileMap.get(key);
String fcd = fileContentDisposition.replaceAll("%name", key);
fcd = fcd.replaceAll("%fileName", file.getName());
dos.writeBytes(prefix + boundary + newLine);
dos.writeBytes(fcd);
dos.writeBytes(fileContentType);
FileInputStream fstram = new FileInputStream(file);
byte[] buf = new byte[4000];
int len = 0;
while (len != -1) {
len = fstram.read(buf);
if(len>0){
dos.write(buf, 0, len);
}
}
dos.writeBytes(newLine);
fstram.close();
}
for(Iterator iterator = m_paraMap.keySet().iterator();iterator.hasNext();){
String key = (String)iterator.next();
String value = (String)m_paraMap.get(key);
String fcd = paramContentDisposition.replaceAll("%name", key);
dos.writeBytes(prefix + boundary + newLine);
dos.writeBytes(fcd);
dos.writeBytes(value);
dos.writeBytes(newLine);
}
dos.writeBytes(prefix + boundary + prefix + newLine);
dos.close();
}
catch(Exception e){
e.printStackTrace();
throw e;
}
}
public InputStream getInputStream() throws Exception{
if(uc==null){
return null;
}
return uc.getInputStream();
}
public static void main(String[] args) throws Exception {
HttpRequestMaker httpRequestMaker= new HttpFileUpoad("http://192.168.1.117:8080/test");
httpRequestMaker.AddFile("file0", "c:/work/test0.xml");
httpRequestMaker.AddFile("file1", "c:/work/test1.xml");
httpRequestMaker.AddParam("p1", "testvalue");
httpRequestMaker.Upload();
InputStream is = httpRequestMaker.getInputStream(); //
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = 0;
while((i = bis.read()) != -1){
baos.write(i);
}
System.out.println(baos.toString());
bis.close();
is.close();
}
/**
*
* @param timeout
*/
public void setReadTimeout(int timeout){
if(uc == null){
return;
}
uc.setReadTimeout(timeout);
}
}