请麻烦各位高手帮忙看看代码的问题在哪儿
其实最奇怪的是,明明上午的时候都好好的,start服务器了之后用
http://loqp9mwigkqiwny:8080/mServer/download.do?itemid=pic
就能找到对应文件夹里叫做pic的文件,然后进行下载。
但是到了下午之后,每次下载的时候都会卡在95左右的位置,有的时候可以下载下来,但是下来的文件都是损坏的。。
小弟就是觉得十分蹊跷一件事情,之前都好好的,突然就下不了了。。我顺便把controller的代码给贴上来吧,各位高手帮我看看,麻烦了~~~package hk.hku.msc.http.controller;import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import hk.hku.msc.http.service.DownloadService;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;@Controller
public class DownloadController { private DownloadService downloadService;
@RequestMapping(params="itemid",value="/download.do")
public void handleDownload(@RequestParam("itemid") String itemid,
HttpServletRequest request,HttpServletResponse response) throws Exception{
System.out.println("--- downlond itemid "+itemid+" ---");
Long start = System.currentTimeMillis();
String dir = "C://Users//Administrator//Desktop//";
String fileName = itemid+".zip";
String fullFileName = dir + fileName;
File file = new File(fullFileName);
if (!file.exists()) {
return;
}
FileInputStream in = null;
InputStreamReader isr = null;
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + file.getName());
response.addHeader("Content-Length", "" + file.length());
response.setContentType("bin");
OutputStream toClient;
String s = null;
StringBuilder sb = new StringBuilder();
try {
in = new FileInputStream(file);
isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
while ((s = br.readLine()) != null) {
sb.append(s);
// sb.append("/r/n");
}
isr.close();
in.close();
toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(sb.toString().getBytes());
toClient.flush();
toClient.close();
} catch (Exception e) {
e.printStackTrace();
}
Long end = System.currentTimeMillis();
System.out.println("-- Download finish, time cost "+ (end-start) +" --");
}
}
其实最奇怪的是,明明上午的时候都好好的,start服务器了之后用
http://loqp9mwigkqiwny:8080/mServer/download.do?itemid=pic
就能找到对应文件夹里叫做pic的文件,然后进行下载。
但是到了下午之后,每次下载的时候都会卡在95左右的位置,有的时候可以下载下来,但是下来的文件都是损坏的。。
小弟就是觉得十分蹊跷一件事情,之前都好好的,突然就下不了了。。我顺便把controller的代码给贴上来吧,各位高手帮我看看,麻烦了~~~package hk.hku.msc.http.controller;import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import hk.hku.msc.http.service.DownloadService;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;@Controller
public class DownloadController { private DownloadService downloadService;
@RequestMapping(params="itemid",value="/download.do")
public void handleDownload(@RequestParam("itemid") String itemid,
HttpServletRequest request,HttpServletResponse response) throws Exception{
System.out.println("--- downlond itemid "+itemid+" ---");
Long start = System.currentTimeMillis();
String dir = "C://Users//Administrator//Desktop//";
String fileName = itemid+".zip";
String fullFileName = dir + fileName;
File file = new File(fullFileName);
if (!file.exists()) {
return;
}
FileInputStream in = null;
InputStreamReader isr = null;
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + file.getName());
response.addHeader("Content-Length", "" + file.length());
response.setContentType("bin");
OutputStream toClient;
String s = null;
StringBuilder sb = new StringBuilder();
try {
in = new FileInputStream(file);
isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
while ((s = br.readLine()) != null) {
sb.append(s);
// sb.append("/r/n");
}
isr.close();
in.close();
toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(sb.toString().getBytes());
toClient.flush();
toClient.close();
} catch (Exception e) {
e.printStackTrace();
}
Long end = System.currentTimeMillis();
System.out.println("-- Download finish, time cost "+ (end-start) +" --");
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货