这里我的下载地址和上传地址都是我自己的服务端
一.文件的下载
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by Administrator on 2016/7/26.
*/
public class DownLoadDemo1 {
private static final String DOWNLOADPATH="http://localhost:8080/day14servletdemo/baidu.exe";//文件的下载地址
private static final String FILEPATH="D:/gpxiazai";
public static void main(String[] args) {
downLoadFile(DOWNLOADPATH,FILEPATH);
}
/**
* /**
* 下载文件,和普通的get请求没有任何区别, 就是返回的流最终转成什么
* 下载文件是get请求,所以知道地址直接请求即可
*
* @param path 网址
* @param filePath 保存的目录(文件夹)
*/
private static void downLoadFile(String path,String filePath) {
//1 创建URL
try {
URL mURL= new URL(path);//根据文件的下载地址创建URL
HttpURLConnection mHttpURLConnection= (HttpURLConnection) mURL.openConnection();//
mHttpURLConnection.setConnectTimeout(5000);//设置连接的超时时间
mHttpURLConnection.setReadTimeout(5000);//设置读取数据的超时时间
mHttpURLConnection.setRequestMethod("GET");//设置请求方式为get 默认其实就是get
mHttpURLConnection.connect();//正式请求, 相当于浏览器里面的回车
if (mHttpURLConnection.getResponseCode()==HttpURLConnection.HTTP_OK){//错误码为200 OK 连接成功
//获取流
InputStream mInputStream= mHttpURLConnection.getInputStream();//获取输入流
String fileName =path.substring(path.lastIndexOf("/")+1);//从最后一个/后面开始
File dir = new File(filePath);
if (!dir.exists()){
dir.mkdir();//创建文件夹
}
File file = new File(dir,fileName);//创建文件
FileOutputStream fos = new FileOutputStream(file);
int len =0;
byte[] bs = new byte[1024*8];//这里不是越大越好,因为网络请求受网络状态限制,太大会等待太久才写入
while ((len=mInputStream.read(bs))!=-1){
fos.write(bs,0,len);
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
二.文件的上传
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.*;
/**
* Created by Administrator on 2016/7/26.
*/
public class UpLoadDemo {
private static final String FILEPATH="C:/Users/Administrator/Desktop/plugin.jar";//要上传的文件
private static final String SERVERPATH="http://localhost:8080/day14servletdemo/upload.html";//服务器地址
public static void main(String[] args) throws IOException {
upLoadFile(FILEPATH);
}
private static void upLoadFile(String filePath) throws IOException {
//上传文件
CloseableHttpClient mCloseableHttpClient= HttpClients.createDefault();//创建默认的客户端
HttpPost mHttpPost= new HttpPost(SERVERPATH);
File file = new File(filePath);//创建要上传的文件
//想办法加到请求正文
FileBody mFileBody=new FileBody(file);//将文件加进去
MultipartEntityBuilder builder = MultipartEntityBuilder.create();//创建构造器
builder.addPart("file",mFileBody);//添加文件到实体
HttpEntity mHttpEntity=builder.build();//构造实体
mHttpPost.setEntity(mHttpEntity);//将实体添加到post请求中
CloseableHttpResponse mCloseableHttpResponse=mCloseableHttpClient.execute(mHttpPost);//发送文件并获取返回数据
if (mCloseableHttpResponse.getStatusLine().getStatusCode()==200){
InputStream mInputStream =mCloseableHttpResponse.getEntity().getContent();//获取输入流
BufferedReader mBufferedReader = new BufferedReader(new InputStreamReader(mInputStream));//包装字符流
String line =null;
while ((line=mBufferedReader.readLine())!=null){
System.out.println(line);
}
mBufferedReader.close();
}
}
}