public class download extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String zippath=request.getSession().getServletContext().getRealPath("/uploads/item/aaaaaaaaaV1.6.doc");
System.out.print("----------------"+zippath);
downLoadFile(zippath, response,"0");
}
public static int downLoadFile(String filePath, HttpServletResponse response,String value) {
response.reset();
response.setCharacterEncoding("UTF-8");
try {
String fileName = "";
if (filePath.lastIndexOf("/") > 0) {
fileName = new String(filePath.substring(
filePath.lastIndexOf("/") + 1, filePath.length()));
} else if (filePath.lastIndexOf("\\") > 0) {
fileName = new String(filePath.substring(
filePath.lastIndexOf("\\") + 1, filePath.length()));
}
File file = new File(filePath);
System.out.print("file------------"+file);
if (file.isFile() && file.exists()) {
FileInputStream fis = new FileInputStream(file);
try{
if(value!=null){
if(file.length()<=Long.parseLong(value)){
return 0;
}
fis.skip(Long.parseLong(value));
}
}catch (Exception e) {
fis.skip(0);
}
response.setContentType("application/octet-stream");
response.setHeader(
"Content-Disposition",
"attachment; filename="
+ java.net.URLEncoder.encode(fileName, "utf-8"));
response.setContentLength((int) file.length());
ServletOutputStream out = response.getOutputStream();
byte[] buffer = new byte[1024];
int temp;
while ((temp = fis.read(buffer, 0, 1024)) > 0) {
out.write(buffer, 0, temp);
}
out.flush();
out.close();
fis.close();
if(!file.delete()){
System.gc();
file.delete();
}
return 1;
}
} catch (Exception e) {
}
return 0;
}
}