package com.baitw.struts.utils;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class FileDownLoad extends HttpServlet {
/**
* @fields serialVersionUID
*/
private static final long serialVersionUID = 1L;
private static final String CONTENT_TYPE = "text/html; charset=GBK";
public void init() throws ServletException {
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
OutputStream out = response.getOutputStream();
request.setCharacterEncoding("GBK");
response.setContentType("application/octet-stream;charset=iso-8859-1");
response.setHeader(
"Content-disposition",
"attachment;filename=\""
+ java.net.URLEncoder.encode(
new String("zzbus.apk".getBytes("iso-8859-1")),
"UTF-8") + "\"");
FileInputStream fis = null;
try {
fis = new FileInputStream(
"D:\\6.0.35\\webapps\\ROOT\\software\\zzbus.apk");
byte[] buffer = new byte[1024];
int byteRead = -1;
while ((byteRead = fis.read(buffer)) != -1) {
out.write(buffer, 0, byteRead);
}
out.flush();
} catch (Exception ex) {
response.setContentType(CONTENT_TYPE);
response.setHeader("Content-disposition", "inline");
out.write("文件未找到".getBytes());
out.close();
ex.printStackTrace();
}
if (fis != null) {
fis.close();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(req, resp);
}
public void destroy() {
}
}
package com.findjar.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class FileDownLoad extends HttpServlet {
/**
* @fields serialVersionUID
*/
private static final long serialVersionUID = 1L;
private static final String CONTENT_TYPE = "text/html; charset=GBK";
public void init() throws ServletException {
}
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String src_name = (String) request.getParameter("src");
String path = request.getParameter("path");
String realPath = request.getRealPath("resources");
if (path != null) {
realPath = request.getRealPath(path);
}
StringBuffer sb = new StringBuffer(realPath);
sb.append("/");
sb.append(src_name);
String dst_name = (String) request.getParameter("dst");
OutputStream out = response.getOutputStream();
request.setCharacterEncoding("GBK");
response.setContentType("application/octet-stream;charset=iso-8859-1");
response.setHeader(
"Content-disposition",
"attachment;filename=\""
+ java.net.URLEncoder.encode(
new String(dst_name.getBytes("iso-8859-1")),
"UTF-8") + "\"");
FileInputStream fis = null;
try {
fis = new FileInputStream(sb.toString());
byte[] buffer = new byte[1024];
int byteRead = -1;
while ((byteRead = fis.read(buffer)) != -1) {
out.write(buffer, 0, byteRead);
}
out.flush();
} catch (Exception ex) {
response.setContentType(CONTENT_TYPE);
response.setHeader("Content-disposition", "inline");
out.write("文件未找到".getBytes());
out.close();
ex.printStackTrace();
}
if (fis != null) {
fis.close();
}
}
public void destroy() {
}
}