打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
文件下载的java
package com.upload.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DownloadServlet extends HttpServlet {

private static final long serialVersionUID = -6748091423171929746L;
static final private String CONTENT_TYPE = "text/html; charset=UTF-8";

// Initialize global variables
public void init() throws ServletException {
}

// Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}

// Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);

String downloadfile = new String(
(request.getParameter("downFile")).getBytes("iso8859-1"),
"UTF-8");
String downloadfileName = request.getParameter("downName");

ServletContext context = getServletContext();

String downloadpath = context.getRealPath(File.separator);
// 构建下载文件
java.io.File file = new java.io.File(downloadpath + downloadfile);

if (!file.exists()) {
response.sendRedirect("no_file.jsp");
} else {
long filesize = file.length();
// 设置输出格式
String fileType = "." + getFiletype(downloadfile);
String type = "";

if (downloadfileName.lastIndexOf(".") != -1) {
type = downloadfileName.substring(
downloadfileName.lastIndexOf("."),
downloadfileName.length());
}

response.addHeader("content-type", "application/x-msdownload;");
if (type != null && fileType.equals(type)) {
response.addHeader(
"Content-Disposition",
"attachment; filename="
+ new String(downloadfileName
.getBytes("utf-8"), "iso8859-1"));
} else {
response.addHeader(
"Content-Disposition",
"attachment; filename="
+ new String((downloadfileName + fileType)
.getBytes("utf-8"), "iso8859-1"));
}

response.addHeader("content-length", Long.toString(filesize));
// 向客户端写入文件
FileInputStream fin = new FileInputStream(file);
byte[] b = new byte[1];
int j = 0;
while ((j = fin.read(b)) > 0) {
response.getOutputStream().write(b, 0, j);
}
fin.close();
}
}

// Clean up resources
public void destroy() {
}

// 取文件名后缀
private static String getFiletype(String fileName) {

String type = "";
if (fileName == null || fileName.equals(""))
return type;
int position = fileName.lastIndexOf(".");
if (position != -1) {
type = fileName.substring(position + 1, fileName.length());
}
return type;
}
}




在jsp页面中调用 :
下载的脚本

function downLoad(path,downName)
{
    document.getElementById("downFile").value=path;
    document.getElementById("downName").value=downName;
    document.downLoadFrm.submit();
}


<下面是一个form表单>
 <form action="<%=downloadServlet%>" id="downLoadFrm" name="downLoadFrm"  method="get" target="downIframe" style="margin: 0px;padding: 0px;height: 0px;width: 0px;">
       <input type="hidden" id="downFile" name="downFile" />
       <input type="hidden" id="downName" name="downName" />
   </form>
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
jsp文件下载完整方法
韩顺平2011细说Servlet笔记2
servlet实现文件下载
Java Servlet实现文件下载 可解决文件名中文乱码
json前后台传值
java网络编程-Ajax+servlet实例
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服