日期:2014-05-17 浏览次数:20721 次
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
if(conn !=null){
try{
conn.setConnectTimeout(60000); //连接超时时间:单位毫秒
conn.setReadTimeout(300000); //读取数据超时时间:单位毫秒
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("patternCommand", patternCommand);
conn.setRequestProperty("ftpPath", ftpPath);
is = conn.getInputStream();
byte[] buffer = new byte[2048];
int count = 0;
try {
while (true) {
int bytesReceived = is.read(buffer);
response.setHeader("Content-Disposition", "filename="+ shortName);
response.setHeader("Connection", "Keep-Alive");
response.setContentType("application/octet-stream");
out = response.getOutputStream();
}
if (bytesReceived < 1) {
break;
}
out.write(buffer, 0, bytesReceived);
out.flush();
count++;
}
} finally {
if(out!=null)out.close();
if(is!=null)is.close();
}
}finally{
if(conn!=null)conn.disconnect();
}
OutputStream os = null;
FileInputStream fis = null;
try {
if(StringUtils.isNotEmpty(outPutPath)){
File file = new File(outPutPath);
os = response.getOutputStream();
fis = FileUtils.openInputStream(file);
byte[] buffer = new byte[2048];
try {
while (true) {
int bytesReceived = fis.read(buffer);
if (bytesReceived < 1) {
break;
}
os.write(buffer, 0, bytesReceived);
os.flush();
}
} finally {
if(os!=null)os.close();
if(fis!=null)fis.close();
}
}
} catch (IOException e) {
log.error(e.getMessage());
e.printStackTrace();<