首页 javaWEB java 上传文件和下载文件 工具类

java 上传文件和下载文件 工具类

public static void downloadFile(String fileName, String down…

public static void downloadFile(String fileName, String downloadName, HttpServletResponse response){
		try {
			response.setContentType("octets/stream");
			response.addHeader("Content-Type", "text/html; charset=utf-8");
			String downLoadName = new String(downloadName.getBytes("gbk"), "iso8859-1");
			response.addHeader("Content-Disposition", "attachment;filename=" + downLoadName);

			FileInputStream fileInputStream = new FileInputStream(fileName);
			OutputStream out = response.getOutputStream();
			int i = 0;
			while ((i = fileInputStream.read()) != -1) {
				out.write(i);
			}
			fileInputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 上传文件
	 *
	 * @param file
	 *            文件
	 * @param radomfileName
	 *            保存文件名
	 * @param materialFilePath
	 *            保存路径
	 * @return
	 */
	public static boolean fileUpload(MultipartFile file, String radomfileName,
			String materialFilePath) {
		boolean flag = true;
		File baseFile = new File(materialFilePath);
		File trainFile = new File(baseFile, radomfileName);
		if (!baseFile.exists()) {
			baseFile.mkdirs();
		}
		try {
			file.transferTo(trainFile);
		} catch (IllegalStateException e) {
			flag = false;
		} catch (IOException e) {
			flag = false;
		}// 保存文件
		return flag;
	}

 

免责声明:文章内容不代表本站立场,本站不对其内容的真实性、完整性、准确性给予任何担保、暗示和承诺,仅供读者参考,文章版权归原作者所有。如本文内容影响到您的合法权益(内容、图片等),请及时联系本站,我们会及时删除处理。

为您推荐

nodejs 整理记录

nodejs 整理记录

下载包 https://blog.csdn.net/m0_59878114/article/details/120274...
websocket测试html

websocket测试html

<!DOCTYPE html> <html> <head> <meta cha...
bigdemical两个数比较大小

bigdemical两个数比较大小

/*int result = bigdemical1.compareTo(bigdemical2) result = -...
Beetl2.7 中文文档

Beetl2.7 中文文档

Beetl目前版本是2.7.23,相对于其他java模板引擎,具有功能齐全,语法直观,性能超高,以及编写的模板容易维护等...
纯CSS实现多个便签在一行展示,拖动滚动

纯CSS实现多个便签在一行展示,拖动滚动

div <h2>请注意需要在移动端预览,PC端拖拽无效果</h2> <div class=...
返回顶部