当前位置: 编程技术>移动开发
本页文章导读:
▪使用缓存有关问题 使用缓存问题
public class CacheManager {
private static final long MAX_SIZE = 5242880L; // 5MB
private CacheManager() {
}
public static void cacheData(Context context, byte[] data, String name) throws IOException {
.........
▪ 一是否 1是否
飞洒四谛法发大水放大
......
▪ Java兑现HTTP文件下载和图片(验证码)浏览 Java实现HTTP文件下载和图片(验证码)浏览
1、实现HTTP文件下载
原理:设置HTTP头信息content-type=”application/x-download;charset=gbk”,然后将文件作为字节写入即可。源码如下:
response.reset(); //设.........
[1]使用缓存有关问题
来源: 互联网 发布时间: 2014-02-18
使用缓存问题
public class CacheManager {
private static final long MAX_SIZE = 5242880L; // 5MB
private CacheManager() {
}
public static void cacheData(Context context, byte[] data, String name) throws IOException {
File cacheDir = context.getCacheDir();
long size = getDirSize(cacheDir);
long newSize = data.length + size;
if (newSize > MAX_SIZE) {
cleanDir(cacheDir, newSize - MAX_SIZE);
}
File file = new File(cacheDir, name);
FileOutputStream os = new FileOutputStream(file);
try {
os.write(data);
}
finally {
os.flush();
os.close();
}
}
public static byte[] retrieveData(Context context, String name) throws IOException {
File cacheDir = context.getCacheDir();
File file = new File(cacheDir, name);
if (!file.exists()) {
// Data doesn't exist
return null;
}
byte[] data = new byte[(int) file.length()];
FileInputStream is = new FileInputStream(file);
try {
is.read(data);
}
finally {
is.close();
}
return data;
}
private static void cleanDir(File dir, long bytes) {
long bytesDeleted = 0;
File[] files = dir.listFiles();
for (File file : files) {
bytesDeleted += file.length();
file.delete();
if (bytesDeleted >= bytes) {
break;
}
}
}
private static long getDirSize(File dir) {
long size = 0;
File[] files = dir.listFiles();
for (File file : files) {
if (file.isFile()) {
size += file.length();
}
}
return size;
}
}
[2] 一是否
来源: 互联网 发布时间: 2014-02-18
1是否
飞洒四谛法发大水放大
[3] Java兑现HTTP文件下载和图片(验证码)浏览
来源: 互联网 发布时间: 2014-02-18
Java实现HTTP文件下载和图片(验证码)浏览
1、实现HTTP文件下载
原理:设置HTTP头信息content-type=”application/x-download;charset=gbk”,然后将文件作为字节写入即可。源码如下:
response.reset(); //设置类型:下载 response.setContentType("application/x-download;charset=gbk"); //文件名称 response.setHeader("Content-Disposition", "attachment;filename=temp.doc"); //写入字节 response.getOutputStream().write(byte[]); response.getOutputStream().flush(); response.getOutputStream().close();
2、图片浏览
客户端: 服务器端: response.setContentType("image/jpeg"); response.getOutputStream().write(byte[]);
原文地址:Java实现HTTP文件下载和图片(验证码)浏览 | http://orgcent.com/java-http-download/
最新技术文章: