Servlet文件下载

servlet文件下载

/**
 * @author Administrator
 * 文件下载
 */
public class DownloadServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		
		//获取uuidRealFileName
		String uuidRealFileName = request.getParameter("uuidRealFileName");
		//还原
		byte[] buf = uuidRealFileName.getBytes("ISO8859-1");
		//手工解码
		uuidRealFileName = new String(buf,"UTF-8");
		//获取realFileName
		int index = uuidRealFileName.indexOf("_");
		String realFileName = uuidRealFileName.substring(index+1);
		//通知浏览器以下载方式打开文件
		//response.setHeader("content-type","application/x-msdownload");
		response.setHeader("content-disposition","attachment;filename="+URLEncoder.encode(realFileName,"UTF-8"));
		//以IO流方式将服务端的文件复制给客户端
		String uploadPath = this.getServletContext().getRealPath("/WEB-INF/upload");
		String subUploadPath = makeSubUpload(uploadPath,uuidRealFileName);
		InputStream is = new FileInputStream(subUploadPath + "/" + uuidRealFileName);
		OutputStream os = response.getOutputStream();
		buf = new byte[2048];
		int len = 0;
		while((len=is.read(buf))>0){
			os.write(buf,0,len);
		}
		os.close();
		is.close();
	}
	//通过这个算法,得到下载文件的位置
	private String makeSubUpload(String uploadPath,String uuidRealFileName){
		//获取hashCode整型值
		int code = uuidRealFileName.hashCode();
		//第一级子目录
		int dir1 = code & 0xF;//12
		//第二级子目录
		int dir2 = ( code >> 1 ) & 0xF;//6 
		//创建这些子目录
		File file = new File(uploadPath+"/"+dir1+"/"+dir2);
		//如果不存在该子目录
		if(!file.exists()){
			//连续创建2个子目录
			file.mkdirs();
		}
		//将创建后的子目录返回
		return file.getPath();
	} 
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Servlet文件上传功能可以通过使用注解@MultipartConfig将Servlet标识为支持文件上传,然后将multipart/form-data的POST请求封装成Part对象,通过Part对象对上传的文件进行操作。以下是一个文件上传的Servlet示例代码: ```java @WebServlet("/uploadServlet") @MultipartConfig // 如果是文件上传,必须要设置该注解! public class UploadServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("文件上传..."); // 设置请求的编码格式 req.setCharacterEncoding("UTF-8"); // 获取普通表单项(获取参数) String uname = req.getParameter("uname"); // 表单中表单元素的name属性值 System.out.println("uname: " + uname); // 获取Part对象(Servlet将multipart/form-data的POST请求封装成Part对象) Part part = req.getPart("myfile"); // 通过Part对象得到上传的文件名 String fileName = part.getSubmittedFileName(); System.out.println("上传文件名:" + fileName); // 得到文件存放的路径 String filePath = req.getServletContext().getRealPath("/"); System.out.println("文件存放路径:" + filePath); // 上传文件到指定目录 part.write(filePath + "/" + fileName); } } ``` 而文件下载功能可以通过设置download属性来实现。以下是一个文件下载的JSP页面的示例代码: ```html <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>文件下载</title> </head> <body> <!-- 浏览器能够识别的资源 --> <a href="download/hello.txt">文本文件</a> <a href="download/pic.jpg">图片文件</a> <!-- 浏览器不能够识别的资源 --> <a href="download/zzz.rar">压缩文件</a> <hr> <a href="download/hello.txt" download>文本文件</a> <a href="download/pic.jpg" download="test.png">图片文件</a> <hr> <form action="downloadServlet"> 文件名:<input type="text" name="fileName" placeholder="请输入要下载的文件名"> <button>下载</button> </form> </body> </html> ``` 以上是文件上传和下载的实现方法,你可以根据需要进行调整和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值