上传
public String upLoda(String urlpath,String filepath)
{
String BOUNDARY = UUID.randomUUID().toString();
String PREFIX = "--" , LINE_END = "\r\n";
String CONTENT_TYPE = "multipart/form-data";
try {
URL url = new URL(urlpath);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setReadTimeout(5000);
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.setRequestProperty("Content-Type",CONTENT_TYPE +";boundary="+ BOUNDARY);
httpURLConnection.connect();
File file = new File(filepath);
if (file!=null)
{
OutputStream outputStream = httpURLConnection.getOutputStream();
StringBuffer stringBuffer = new StringBuffer();
//真正数据流发送前进行边界设置
stringBuffer.append(PREFIX);
stringBuffer.append(BOUNDARY);
stringBuffer.append(LINE_END);
stringBuffer.append("Content-Disposition: form-data; name=\"file\"; filename=\""+file.getName()+"\""+LINE_END);
stringBuffer.append(LINE_END);
outputStream.write(stringBuffer.toString().getBytes());
//真数据流部分
InputStream inputStream = new FileInputStream(file);
byte[] bytes = new byte[1024];
int len = 0;
while ((len=inputStream.read(bytes))!=-1)
{
outputStream.write(bytes,0,len);
}
inputStream.close();
//真正数据流完毕需要设定结尾标记
outputStream.write(LINE_END.getBytes());
byte[] enddata = (PREFIX+BOUNDARY+PREFIX+LINE_END).getBytes();
outputStream.write(enddata);
outputStream.flush();
if (httpURLConnection.getResponseCode()==200)
{
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected String doInBackground(String... strings) {
// String string = HtppUtils.getInstance().getString(strings[0]);
// String strings1 = strings[0];
// String string2 = strings[1];
// String s = HtppUtils.getInstance().PostString(strings1, string2);
// Log.i("sss", "doInBackground: "+s+string2);
// String s = HtppUtils.getInstance().DownLoad(strings[0]);
String urlpath = strings[0];
String filepath = strings[1];
HtppUtils.getInstance().upLoda(urlpath,filepath);
return null;
}
public void upload(View view) {
new MyAsyncTask().execute("http://10.161.11.62/nihao/","/sdcard/hh.jpg");
}
记得开启权限
下载
public void download(View view) {
new MyAsyncTask().execute("https://img0.baidu.com/it/u=1684532727,1424929765&fm=253&fmt=auto&app=120&f=JPEG?w=1200&h=675");
}
public String DownLoad(String path)
{
try {
URL url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setReadTimeout(2000);
httpURLConnection.setConnectTimeout(2000);
httpURLConnection.connect();
if (httpURLConnection.getResponseCode()==200)
{
int len = 0;
byte[] buffer = new byte[1024];
InputStream inputStream = httpURLConnection.getInputStream();
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
File downloadCacheDirectory = Environment.getExternalStorageDirectory();
File file = new File(downloadCacheDirectory,"hh.jpg");
FileOutputStream fileOutputStream = new FileOutputStream(file);
while ((len = inputStream.read(buffer))!=-1)
{
fileOutputStream.write(buffer,0,len);
}
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected String doInBackground(String... strings) {
// String string = HtppUtils.getInstance().getString(strings[0]);
// String strings1 = strings[0];
// String string2 = strings[1];
// String s = HtppUtils.getInstance().PostString(strings1, string2);
// Log.i("sss", "doInBackground: "+s+string2);
String s = HtppUtils.getInstance().DownLoad(strings[0]);
return null;
}
GET获取数据
public String getString(String path)
{
try {
URL url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setReadTimeout(2000);
httpURLConnection.setConnectTimeout(2000);
httpURLConnection.connect();
if (httpURLConnection.getResponseCode()==200)
{
// InputStream inputStream = httpURLConnection.getInputStream();
// BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
// String str="";
// StringBuilder stringBuilder = new StringBuilder();
// while ((str=bufferedReader.readLine())!=null)
// {
// stringBuilder.append(str);
// }
InputStream inputStream = httpURLConnection.getInputStream();
int len = 0;
byte[] buffer = new byte[1024];
StringBuilder stringBuilder = new StringBuilder();
while ((len=inputStream.read(buffer))!=-1)
{
stringBuilder.append(new String(buffer,0,len));
}
Log.i("sss", "doInBackground: "+stringBuilder.toString());
return stringBuilder.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
POST获取数据
public String PostString(String path,String params)
{
try {
URL url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setReadTimeout(2000);
httpURLConnection.setConnectTimeout(2000);
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.connect();
OutputStream outputStream = httpURLConnection.getOutputStream();
outputStream.write(params.getBytes());
outputStream.flush();
if (httpURLConnection.getResponseCode()==200)
{
byte[] bytes = new byte[1024];
int len = 0;
InputStream inputStream = httpURLConnection.getInputStream();
StringBuilder stringBuilder = new StringBuilder();
while ((len = inputStream.read(bytes))!=-1)
{
stringBuilder.append(new String(bytes,0,len));
}
return stringBuilder.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
get和post请求的区别
1.GET在浏览器回退时是无害的,而POST会再次提交请求。
2.GET产生的URL地址可以被Bookmark(书签),而POST不可以。
3.GET请求会被浏览器主动cache,而POST不会,除非手动设置。
4.GET请求只能进行url编码,而POST支持多种编码方式。
5.GET请求参数会被完整保留在浏览器历史记录里,而POST中的参数不会别保留。
6.GET请求在URL中传送的参数是有长度限制的,而POST没有。
7.对参数的数据类型,GET只能接受ASCLL字符,而POST没有限制。
8.GET比POST更不安全,因为参数直接暴露在URL上,所有不能用来传递敏感信息。
9.GET参数通过URL传递,POST放在Request body中。
该代码示例展示了在Android中使用HttpURLConnection进行文件上传和下载的方法。上传通过POST请求实现,设置multipart/form-data内容类型,添加文件边界并读取文件流。下载则通过GET请求获取服务器资源,写入到本地文件。
1649

被折叠的 条评论
为什么被折叠?



