上传下载工具类

1、下载

public static void downloadFile(HttpServletResponse response, String filepath,
			String fileName) throws IOException {
		try {
			File file = new File(path.concat(filepath));
			if (StringUtils.isBlank(fileName)) {
				fileName = file.getName();
			}
			response.addHeader("Content-Disposition","attachment; filename="+ new String(fileName.getBytes("GB2312"), "ISO-8859-1")); 
			response.setContentType("application/x-msdownload");
			response.setContentLength((int) file.length());
			InputStream inputStream = new FileInputStream(file);
			OutputStream os = response.getOutputStream();
			byte[] b = new byte[1024];
			int length;
			while ((length = inputStream.read(b)) > 0) {
				os.write(b, 0, length);
			}
			inputStream.close();
			os.close();
		} catch (FileNotFoundException e) {
			log.error("文件找不到", e);
		} catch (IOException e) {
			log.error("io异常", e);
		}
	}

猜你喜欢

转载自blog.csdn.net/xingyuncaojun/article/details/85604569
今日推荐