请问怎么用java模仿客户端来实现http的put请求上传一个文件到服务器上
http://expert.csdn.net/Expert/topic/2238/2238658.xml?temp=1.171291E-03
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class SimulateUpload
{
private static final byte[] BOUNDARY = "---------------------------7d33bc1b47038c".getBytes();
private static final byte[] CRLF = "\r\n".getBytes();
public void simUpload(String page, String[] paramNames, Object[] paramValues, OutputStream os) throws IOException
{
int contentLength = calculateLength(paramNames, paramValues);
os.write(("POST " + page + " HTTP/1.1").getBytes());
os.write(CRLF);
os.write("User-Agent: myselfHttp/1.0".getBytes());
os.write(CRLF);
os.write("Accept: */*".getBytes());
os.write(CRLF);
os.write("Host: ".getBytes());
os.write(InetAddress.getLocalHost().getHostAddress().getBytes());
os.write(CRLF);
os.write("Content-Type: multipart/form-data; boundary=".getBytes());
os.write(BOUNDARY);
os.write(CRLF);
os.write(("Content-Length: " + contentLength).getBytes());
os.write(CRLF);
os.write(CRLF);
os.write(BOUNDARY);
os.write(CRLF);
for(int i=0; i<paramNames.length; i++)
{
if(paramValues[i] instanceof String)
writeParameter(os, paramNames[i], (String) paramValues[i]);
else if(paramValues[i] instanceof File)
writeFile(os, paramNames[i], (File) paramValues[i]);
}
os.flush();
}
private void writeFile(OutputStream os, String paramName, File file) throws IOException
{
os.write("Content-Disposition: form-data; name=\"".getBytes());
os.write(paramName.getBytes());
os.write("\"; filename=\"".getBytes());
os.write(file.getAbsolutePath().getBytes());
os.write("\"\r\nContent-Type: application/octet-stream".getBytes());
os.write(CRLF);
os.write(CRLF);
FileInputStream fis = new FileInputStream(file);
byte[] buf = new byte[4096];
int bytes = 0;
while((bytes = fis.read(buf)) != -1)
os.write(buf, 0, bytes);
fis.close();
os.write(CRLF);
os.write(BOUNDARY);
os.write(CRLF);
}
private void writeParameter(OutputStream os, String name, String value) throws IOException
{
os.write("Content-Disposition: form-data; name=\"".getBytes());
os.write(name.getBytes());
os.write(");
os.write(CRLF);
os.write(CRLF);
os.write(value.getBytes());
os.write(CRLF);
os.write(BOUNDARY);
os.write(CRLF);
}
private int calculateLength(String[] paramNames, Object[] paramValues)
{
int result = BOUNDARY.length + CRLF.length;
for(int i=0; i<paramNames.length; i++)
{
if(paramValues[i] instanceof String)
result += 88 + paramNames[i].getBytes().length + ((String)paramValues[i]).getBytes().length;
else if(paramValues[i] instanceof File)
{
File file = (File) paramValues[i];
result += 141 + paramNames[i].getBytes().length + file.getAbsolutePath().getBytes().length + file.length();
}
}
return result;
}
public static void main(String[] args)
{
try
{
Socket socket = new Socket("localhost", 80);
OutputStream os = socket.getOutputStream();
SimulateUpload sim = new SimulateUpload();
String[] paramNames = new String[]{"param1", "param2", "param3", "file", "file2"};
Object[] paramValues = new Object[]{"value1", "value2", "value3", new File("d:\\test.txt"), new File("d:\\test1.txt")};
sim.simUpload("/uploadServlet", paramNames, paramValues, os);
os.flush();
os.close();
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}