http下载文件

InputStream bis = null;
		BufferedOutputStream bos = null;
		
		try {
			response.setContentType("UTF-8");
			response.setCharacterEncoding("UTF-8");
			
//			if (uploadFile.getExtend().equals("text")) {
//				response.setContentType("text/plain;");
//			} else if (uploadFile.getExtend().equals("doc")) {
//				response.setContentType("application/msword;");
//			} else if (uploadFile.getExtend().equals("xls")) {
//				response.setContentType("application/ms-excel;");
//			} else if (uploadFile.getExtend().equals("pdf")) {
//				response.setContentType("application/pdf;");
//			} else if (uploadFile.getExtend().equals("jpg") || uploadFile.getExtend().equals("jpeg")) {
//				response.setContentType("image/jpeg;");
//			} else {
//				response.setContentType("application/x-msdownload;");
//			}
			response.setContentType("text/plain;");
			
			response.setHeader("Content-disposition", "attachment; filename=menu.json");
			response.setHeader("Content-Length", ""+(new File(fileName)).length());
			
			bos = new BufferedOutputStream(response.getOutputStream());
			bis = new BufferedInputStream(new FileInputStream(fileName));
			byte[] buff = new byte[2048];
			int bytesRead;
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}
			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if (bis != null) {
					bis.close();
				}
				if (bos != null) {
					bos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

你可能感兴趣的:(http下载文件)