怎么使用httpclient中的put方法上传大文件?
如果是一次性读到内存中发送的话,内存很容易撑爆,所以肯定是用边读边发的方式,或者使用分片上传。现在想知道怎么使用httpclient实现边读边传,使用jdk自带的方法已经知道怎么实现了。
public static void putRequest(String uri, Map<String, String> headers, File file)
{
URLConnection urlconnection = null;
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try
{
URL url = new URL(uri);
urlconnection = url.openConnection();
urlconnection.setDoOutput(true);
urlconnection.setDoInput(true);
if (urlconnection instanceof HttpURLConnection)
{
try
{
((HttpURLConnection) urlconnection).setRequestMethod("PUT");
for(Map.Entry<String, String> entry : headers.entrySet())
{
((HttpURLConnection) urlconnection).setRequestProperty(entry.getKey(), entry.getValue());
}
((HttpURLConnection) urlconnection).connect();
}
catch (ProtocolException e)
{
e.printStackTrace();
}
}
bos = new BufferedOutputStream(urlconnection.getOutputStream());
bis = new BufferedInputStream(new FileInputStream(file));
byte[] buff = new byte[1024 * 8];
int i;
while ((i = bis.read(buff)) != -1)
{
bos.write(buff, 0, i);
}
System.out.println(((HttpURLConnection) urlconnection).getResponseMessage());
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
IOUtils.closeQuietly(bis);
IOUtils.closeQuietly(bos);
((HttpURLConnection) urlconnection).disconnect();
}
}
Already know how to transmit. The key is to implement the writeTo method in HttpEntity. There are already related implementations in httpclient. You can refer to:
In addition, the implementation of this FileEntity uses the ordinary IO method. Searching can find that the httpcore package has implemented NFileEntity and uses the FileChannel method, which will be more efficient
You can use the streaming method
Do I have to use http protocol?
With a size of 1G, it is no longer recommended to handle it through http.
ftp it.