下载文件Servlet

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() {
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值