来源于之前的博客。
之前在开发Android程序的时候碰到大量下载图片速度非常慢的问题,可以使用图片压缩方法将所有图片压缩,提高传输效率。
服务器端使用servlet的输出流,配合java的压缩工具ZipOutPUtStream和ZipInputStream来完成。
服务器端主要代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.reset();
response.setContentType("text/xml");
response.setHeader("Content-Disposition",
"attachment; filename="icons.zip"");// 固定下载名称
response.setBufferSize(33554432);
// 得到文件列表,这里可以来自用户选择,也可以是从数据库读取资料,动态生成的报表
String path = "E:/tp";
ZipOutputStream zos = null;
FileInputStream fis = null;
try {
File file = new File(path);//图片文件根目录
zos = new ZipOutputStream(response.getOutputStream());//创建压缩文件的输出流
for(String fileName:file.list()){//从目录文件中提取图片,依次写入压缩文件输出流
zos.putNextEntry(new ZipEntry(fileName));//向压缩文件输出流里添加一个文件
fis = new FileInputStream(path + "/" + fileName);//创建本地磁盘输入流,读入数据
byte inbuf[] = new byte[4096];//读入数据时的缓存
int len;//代表读取的长度
while((len = fis.read(inbuf)) != -1) {//读取成功
zos.write(inbuf, 0, len);//写入输出流
}
zos.closeEntry();//关闭当前输入文件
fis.close();//关闭输入流
fis = null;
}
zos.flush();//整个压缩文件完成,输出数据
response.getOutputStream().flush();
response.getOutputStream().close();
zos.close();
zos = null;
System.out.println("压缩完毕.");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {//关闭未关闭的输出流
try {
if(zos != null) zos.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
Android端代码:
package down.load.icons;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class DownloadIconsActivity extends Activity {
public TextView t;
public Button b;
public Button d;
public String sdPath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
sdPath=Environment.getExternalStorageDirectory()+"/";
Log.v("sd", sdPath);
}
t = (TextView)findViewById(R.id.t);
b = (Button)findViewById(R.id.b);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Date d = new Date();
t.setText("" + d.getHours() + "时" + d.getMinutes() + "分" + d.getSeconds() + "秒");
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
HttpURLConnection hc = null;
try{
// 打开一个http连接
URL url = new URL("http://192.168.0.100:8080/DownloadImages/DownloadImgsZip");
hc = (HttpURLConnection) url.openConnection();
// 定义输入流
InputStream instream = hc.getInputStream();
new File(sdPath + "imgs_down").mkdir();
ZipInputStream inputZip = new ZipInputStream(instream);
ZipEntry zip = null;
byte[] outBuf = new byte[4096];
int outLen;
File image = null;
while( (zip = inputZip.getNextEntry()) != null ){
image = new File(sdPath + "/imgs_down/" + zip.getName());
image.createNewFile();
FileOutputStream out = new FileOutputStream(image);
while( (outLen = inputZip.read(outBuf)) != -1){
out.write(outBuf , 0 , outLen);
}
out.flush();
out.close();
}
hc.disconnect();
} catch (Exception e) {
Log.v("err", "下载出错!" + e.getMessage());
} finally{
if (hc != null)
hc.disconnect();
Date d_sd = new Date();
t.setText(t.getText() + " -- " + d_sd.getHours() + "时" + d_sd.getMinutes() + "分" + d_sd.getSeconds() + "秒");
}
}else{
t.setText("没有存储卡");
}
}
});
Button d = (Button)findViewById(R.id.d);
d.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Date date_f = new Date();
t.setText("" + date_f.getHours() + "时" + date_f.getMinutes() + "分" + date_f.getSeconds() + "秒");
HttpURLConnection hc = null;
File tp = null;
try{
// 打开一个http连接
URL url = new URL("http://192.168.0.127:8080/DownloadImages/tp/tp.rar");
hc = (HttpURLConnection) url.openConnection();
// 定义输入流
InputStream instream = hc.getInputStream();
tp = new File(sdPath + "tykmImg_down/" + "tp.rar");
tp.createNewFile();
FileOutputStream fos = new FileOutputStream(tp);
int file_len;
byte[] buf = new byte[1024];
while((file_len = instream.read(buf, 0, 1024))> 0){
fos.write(buf, 0, file_len);
}
fos.flush();
fos.close();
} catch (Exception e) {
Log.v("err", "下载出错!" + e.getMessage());
} finally{
if (hc != null)
hc.disconnect();
Date date_s = new Date();
t.setText(t.getText() + " -- " + date_s.getHours() + "时" + date_s.getMinutes() + "分" + date_s.getSeconds() + "秒");
}
}
});
}
public File createSDDir(String dirName){
File dir=new File(sdPath + dirName);
System.out.println("storage device's state :"+Environment.getExternalStorageState());
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
System.out.println("this directory real path is:"+dir.getAbsolutePath());
System.out.println("the result of making directory:"+dir.mkdir());
}
return dir;
}
public File createSDFile(String fileName) throws IOException{
File file=new File(sdPath+fileName);
file.createNewFile();
return file;
}
‘
经测试,下载700多张图片仅需要40秒可完成。