package cn.edu.hit.servlet;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FileDownload extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private ServletConfig config;/* 定义配置对象 */
/**
* Constructor of the object.
*/
public FileDownload() {
super();
}
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String filename = request.getParameter("filename");
String path = "c:/download/" + filename;
File file = new File(path);
if(file.exists()) {
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition", "attachment;filename=\""+filename+"\"");
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] bytes = new byte[1024];
ServletOutputStream out = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(out);
int readLength = 0;
while((readLength = bis.read(bytes)) != -1) {
out.write(bytes, 0, readLength);
}
is.close();
bis.close();
bos.flush();
out.close();
bos.close();
}
// SmartUpload mySmartUpload = new SmartUpload();/* 定义SmartUpload对象 */
// mySmartUpload.initialize(config, request, response);/* 初始化配置信息 */
//
// mySmartUpload.setContentDisposition(null);
//
// try {
// mySmartUpload.downloadFile(path, "application/x-msdownload");
//
// Files fs = mySmartUpload.getFiles();
//
// File f = fs.getFile(0);
// String fileName = f.getFileName();
// if(!fileName.equals("")) {
// String downloadPath = "c:/mydownload/"+fileName;
// f.saveAs(downloadPath, SmartUpload.SAVE_VIRTUAL);
// }
// } catch (SmartUploadException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
response.sendRedirect("servlet/ShowFileServlet");
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
基于Web的文件传输网络应用的开发
需积分: 9 201 浏览量
2012-06-25
09:55:05
上传
评论
收藏 26KB RAR 举报
yfnick2013
- 粉丝: 1
- 资源: 8
最新资源
- 351AN-VB一款N-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- 346G-VB一款N-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- 342P-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- 3420-VB一款SOT23封装N-Channel场效应MOS管
- UDP原理详解和代码示例
- 3414-VB一款N-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- 以下是一些大数据面试习题.pdf
- 3414-CMN3414-VB一款SOT23封装N-Channel场效应MOS管
- ASTM G154-2023 Practice for Operating Fluorescent Ultraviolet UV
- 小米智能摄像机 云台版2K 固件
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈