表单中上传文件、压缩上传图片
2011-08-19 09:32
309 查看
表单上传文件,详见代码:
public boolean edit(String url, List<NameValuePair> params) { //name是字段名,value是文件地址
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPut httpPut = new HttpPut(url);
try {
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
for (int index = 0; index < params.size(); index++) {
if (params.get(index).getName()
.equalsIgnoreCase("image")) {
entity.addPart(params.get(index).getName(),
new FileBody(ImageUtils.zoomToFix(params.get(index).getValue(), 200))); //zoomToFix是自己封装的压缩上传图片的方法,具体见下文
} else {
entity.addPart(
params.get(index).getName(),
new StringBody(params.get(index).getValue(), Charset.forName("UTF-8")));
}
}
httpPut.setEntity(entity);
HttpResponse response = httpClient.execute(httpPut, localContext);
ReturnMsg<User> msg = getReturnMsg(response.getEntity().getContent());
if(msg != null && msg.getRespCode() == ReturnCode.SUCCESS) {
return true;
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
压缩上传图片:
private static BitmapFactory.Options options = new BitmapFactory.Options();
/**
* 压缩图片为固定高度
* @param filePath 图片地址
* @param h 指定高度
* @return
*/
public static File zoomToFix(String filePath, int h) {
File file = new File(filePath);
try {
options.inJustDecodeBounds = true; // 先设置预读图片,既只读取大小信息
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
options.inJustDecodeBounds = false; // 读出大小信息后改为非预读
int be = options.outWidth/h; // 计算缩放倍数
if(be <= 0) be = 1;
options.inSampleSize = be; // 设置缩放,可惜只能是整数
bitmap = BitmapFactory.decodeFile(filePath, options); // 根据缩放比实际读取图片
FileOutputStream fos = new FileOutputStream(file); // 保存图片文件
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (FileNotFoundException e) {
Log.e("ImageUtils", e.getMessage(), e);
}
return file;
}
public boolean edit(String url, List<NameValuePair> params) { //name是字段名,value是文件地址
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPut httpPut = new HttpPut(url);
try {
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
for (int index = 0; index < params.size(); index++) {
if (params.get(index).getName()
.equalsIgnoreCase("image")) {
entity.addPart(params.get(index).getName(),
new FileBody(ImageUtils.zoomToFix(params.get(index).getValue(), 200))); //zoomToFix是自己封装的压缩上传图片的方法,具体见下文
} else {
entity.addPart(
params.get(index).getName(),
new StringBody(params.get(index).getValue(), Charset.forName("UTF-8")));
}
}
httpPut.setEntity(entity);
HttpResponse response = httpClient.execute(httpPut, localContext);
ReturnMsg<User> msg = getReturnMsg(response.getEntity().getContent());
if(msg != null && msg.getRespCode() == ReturnCode.SUCCESS) {
return true;
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
压缩上传图片:
private static BitmapFactory.Options options = new BitmapFactory.Options();
/**
* 压缩图片为固定高度
* @param filePath 图片地址
* @param h 指定高度
* @return
*/
public static File zoomToFix(String filePath, int h) {
File file = new File(filePath);
try {
options.inJustDecodeBounds = true; // 先设置预读图片,既只读取大小信息
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
options.inJustDecodeBounds = false; // 读出大小信息后改为非预读
int be = options.outWidth/h; // 计算缩放倍数
if(be <= 0) be = 1;
options.inSampleSize = be; // 设置缩放,可惜只能是整数
bitmap = BitmapFactory.decodeFile(filePath, options); // 根据缩放比实际读取图片
FileOutputStream fos = new FileOutputStream(file); // 保存图片文件
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (FileNotFoundException e) {
Log.e("ImageUtils", e.getMessage(), e);
}
return file;
}
相关文章推荐
- 文件上传,服务端压缩文件方法,重点是png与gif图片的压缩,保证了透明度与动画
- Swift使用Alamofire上传图片等FormData表单文件的方法
- NodeJs表单上传图片文件
- jqm文件上传,上传图片,jqm的表单操作,jqm的ajax的使用,jqm文件操作大全,文件操作demo
- java实现文件上传下载和图片压缩代码示例
- OKhttp3.X 3.0上传图片文件及表单MultipartBody
- Applet上传插件,Applet上传控件,图片压缩上传,大文件上传,进度条上传 version:1.5
- ajax中form表单中含有文件上传功能,后台语言是java,包括图片的下载,预览,删除
- 微信公众号页面调用拍照上传图片,包括预览没文件压缩,还有扫码功能
- php使用curl模拟浏览器表单上传文件或者图片的方法
- ssm上传图片文件到ftp(图片压缩处理)
- angularjs客户端实现压缩图片文件并上传实例
- struts2多文件上传(修改名称、压缩图片、删除图片)
- iOS 图片文件上传 ----表单形式
- 一键自动发布ipa(更新svn,拷贝资源,压缩资源,加密图片资源,加密数据文件,加密lua脚本,编译代码,ipa签名,上传ftp)
- android 上传图片前压缩文件
- 如何通过表单上传图片数据,PHP后端获取并且保存文件数据
- 图片上传-form表单还是base64-前端图片压缩
- 项目回顾1-图片上传-form表单还是base64-前端图片压缩
- jsp form表单提交获取返回数据(也可用来上传文件、图片)