Commit f89af689 authored by yanzg's avatar yanzg

压缩处理

parent dd6816b1
...@@ -2,12 +2,14 @@ package com.yanzuoguang.util.helper; ...@@ -2,12 +2,14 @@ package com.yanzuoguang.util.helper;
import com.yanzuoguang.util.exception.CodeException; import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.thread.ProcessData;
import com.yanzuoguang.util.thread.RunProcess;
import java.io.BufferedReader; import java.io.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
/** /**
* HTTP请求工具类 * HTTP请求工具类
...@@ -112,4 +114,47 @@ public class HttpHelper { ...@@ -112,4 +114,47 @@ public class HttpHelper {
} }
return result; return result;
} }
/**
* 下载文件
*
* @param serverFileName 下载的服务器文件路径
* @param localFileName 保存的文件路径
* @throws IOException
*/
public static void downToLocal(String serverFileName, String localFileName) throws IOException {
downToLocal(serverFileName, localFileName, null);
}
/**
* 下载文件
*
* @param serverFileName 下载的服务器文件路径
* @param localFileName 保存的文件路径
* @param runProcess 进度处理
* @throws IOException
*/
public static void downToLocal(String serverFileName, String localFileName, RunProcess runProcess) throws IOException {
int byteRead;
URL url = new URL(serverFileName);
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
try {
ProcessData data = new ProcessData(serverFileName, conn.getContentLengthLong());
FileOutputStream outputStream = new FileOutputStream(localFileName);
try {
byte[] buffer = new byte[1204];
while ((byteRead = inStream.read(buffer)) != -1) {
data.processAdd(runProcess, byteRead);
outputStream.write(buffer, 0, byteRead);
}
} finally {
outputStream.close();
}
} finally {
inStream.close();
}
}
} }
package com.yanzuoguang.util.thread;
import com.yanzuoguang.util.helper.HttpHelper;
import com.yanzuoguang.util.log.Log;
/**
* 进度数据
*/
public class ProcessData {
/**
* 目标
*/
private String target;
/**
* 位置
*/
private long pos;
/**
* 长度
*/
private long total;
public ProcessData() {
}
public ProcessData(String target) {
this.target = target;
}
public ProcessData(String target, long total) {
this.target = target;
this.total = total;
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public long getPos() {
return pos;
}
public void setPos(long pos) {
this.pos = pos;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
/**
* 进度调用
*
* @param runProcess
* @param pos
*/
public void processCall(RunProcess runProcess, long pos) {
this.pos = pos;
if (runProcess == null) {
return;
}
try {
runProcess.execute(this);
} catch (Exception ex) {
Log.error(HttpHelper.class, ex);
}
}
/**
* 进度调用
*
* @param runProcess 运行进度
* @param add 需要添加的量
*/
public void processAdd(RunProcess runProcess, long add) {
this.pos += add;
this.processCall(runProcess, this.pos);
}
}
package com.yanzuoguang.util.thread;
/**
* 运行进度处理
*/
public interface RunProcess {
/**
* 进度信息
*
* @param data
*/
void execute(ProcessData data);
}
package com.yanzuoguang.cloud.helper; package com.yanzuoguang.cloud.helper;
import com.yanzuoguang.util.helper.HttpHelper;
import javax.servlet.ServletOutputStream; import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.*; import java.io.*;
import java.net.URL;
import java.net.URLConnection;
/** /**
* HTTP文件 * HTTP文件
*/ */
public class HttpFileHelper { public class HttpFileHelper extends HttpHelper {
/**
* 下载文件
*
* @param serverFileName 下载的服务器文件路径
* @param localFileName 保存的文件路径
* @throws IOException
*/
public static void downToLocal(String serverFileName, String localFileName) throws IOException {
int byteRead;
URL url = new URL(serverFileName);
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
try {
FileOutputStream outputStream = new FileOutputStream(localFileName);
try {
byte[] buffer = new byte[1204];
while ((byteRead = inStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, byteRead);
}
} finally {
outputStream.close();
}
} finally {
inStream.close();
}
}
/** /**
* 下载文件 * 下载文件
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment