利用response对象使客户端实现下载

	response.setContentType("text/html");
		OutputStream out=response.getOutputStream();
		//得到文件的在服务器中的实际地址
		String filepath=this.getServletContext().getRealPath("/pic/a上海留學生莎莉自拍第一季.jpg");
		String filename=filepath.substring(filepath.lastIndexOf("\\")+1);
		
		//如果url路径含有中文,则需要使用URLEncoder对中文进行重新编码
		response.setHeader("content-disposition",
				"attachment:filename="+URLEncoder.encode(filename,"UTF-8"));
		FileInputStream in=new FileInputStream(filepath);
		byte data[]=new byte[1024*1024];
		in.read(data);
		out.write(data);

你可能感兴趣的:(String,服务器,url,byte)