一、Java中如何图片异步上传?
这里主要介绍java异步上传图片示例,需要的朋友可以参考:
代码如下:
final File imageFile = new File(getCacheDir().getPath() + "/img/" + p.image);
image.setVisibility(View.GONE);
view.findViewById(R.id.imageLoading).setVisibility(View.VISIBLE);
(new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... params) {
try {
Bitmap image;
if (!imageFile.exists() || imageFile.length() == 0) {
image = BitmapFactory.decodeStream(new URL(
"http://example.com/images/"
+ p.image).openStream());
image.compress(Bitmap.CompressFormat.JPEG, 85,
new FileOutputStream(imageFile));
image.recycle();
}
image = BitmapFactory.decodeFile(imageFile.getPath(),
bitmapOptions);
return image;
} catch (MalformedURLException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
return null;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(Bitmap image) {
if (view.getTag() != p) // The view was recycled.
return;
view.findViewById(R.id.imageLoading).setVisibility(
View.GONE);
view.findViewById(R.id.image)
.setVisibility(View.VISIBLE);
((ImageView) view.findViewById(R.id.image))
.setImageBitmap(image);
}
}).execute();
二、java ssm 图片上传
Java + SSM框架搭建的Web应用在实现图片上传功能时是非常常见的需求。图片上传是网站开发中一个基础且重要的功能,因为用户可能需要上传头像、产品图片或其他类型的图片文件。在这篇博客中,我们将介绍如何使用Java和SSM框架来实现图片上传功能。
1. 环境准备
首先,确保你已经搭建好了Java开发环境,并且熟悉SSM框架(Spring + Spring MVC + MyBatis)的基本原理和用法。另外,我们也需要一个Web服务器来部署我们的应用。
2. 上传文件
要实现图片上传功能,首先需要在前端页面添加一个文件上传的表单,用户可以选择本地图片文件并将其上传到服务器。在后端代码中,我们需要编写文件上传的逻辑,包括接收文件、存储文件以及返回文件的访问路径。
3. 前端页面
在前端页面中,我们可以使用的表单元素和标签来实现文件上传功能。通过