您的位置:首页 > 移动开发 > Android开发

android 上传/下载 图片

2015-09-20 18:53 387 查看
public class HttpAssist {

private static final String TAG = "uploadFile";
private static final int TIME_OUT = 10 * 10000000; // 超时时间
private static final String CHARSET = "utf-8"; // 设置编码
public static final String SUCCESS = "1";
public static final String FAILURE = "0";

//上传文件
public static String uploadFile(File file) {

String BOUNDARY = UUID.randomUUID().toString(); // 边界标识 随机生成
String PREFIX = "--", LINE_END = "\r\n";
String CONTENT_TYPE = "multipart/form-data"; // 内容类型
//请求URL
String RequestURL = "http://192.168.0.100:7080/YkyPhoneService/Uploadfile1";
//String RequestURL = "http://www.hong3.com/Fortest/getfiles";
try {
URL url = new URL(RequestURL);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setReadTimeout(TIME_OUT);
conn.setConnectTimeout(TIME_OUT);
conn.setDoInput(true); // 允许输入流
conn.setDoOutput(true); // 允许输出流
// Post 请求不能使用缓存
conn.setUseCaches(false); // 不允许使用缓存
conn.setRequestMethod("POST"); // 请求方式
conn.setRequestProperty("Charset", CHARSET); // 设置编码
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary="
+ BOUNDARY);
if (file != null) {
/**
* 当文件不为空,把文件包装并且上传
*/
OutputStream outputSteam = conn.getOutputStream();

DataOutputStream dos = new DataOutputStream(outputSteam);
StringBuffer sb = new StringBuffer();
sb.append(PREFIX);
sb.append(BOUNDARY);
sb.append(LINE_END);
/**
* 这里重点注意: name里面的值为服务器端需要key 只有这个key 才可以得到对应的文件 ,也就是此处的uploadfile,
* filename是文件的名字,包含后缀名的 比如:abc.png
*/

sb.append("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\""
+ file.getName() + "\"" + LINE_END);
sb.append("Content-Type: application/octet-stream; charset="
+ CHARSET + LINE_END);
sb.append(LINE_END);
dos.write(sb.toString().getBytes());
InputStream is = new FileInputStream(file);
byte[] bytes = new byte[1024];
int len = 0;
while ((len = is.read(bytes)) != -1) {
dos.write(bytes, 0, len);
}
is.close();
dos.write(LINE_END.getBytes());
byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINE_END)
.getBytes();
dos.write(end_data);
dos.flush();
/**
* 获取响应码 200=成功 当响应成功,获取响应的流
*/
int res = conn.getResponseCode();
if (res == 200) {
return SUCCESS;
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return FAILURE;
}

/**
* 从服务器取图片
* @param url
* @return
*/
public static Bitmap getHttpBitmap(String url) {

URL myFileUrl = null;
Bitmap bitmap = null;

try {
myFileUrl = new URL(url);
//myFileUrl = new URL(s);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {

HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setConnectTimeout(0);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.connect();
System.setProperty("http.keepAlive", "false");
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
return bitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息