java文件内容写入并导出文件

try {
			File f=new File(name + ".txt");
			FileOutputStream fos = new FileOutputStream(f);
			fos.write("写入内容".getBytes());
			
			OutputStream out = response.getOutputStream();
			BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
	        byte[] buf = new byte[1024];
	        int len = 0;

			String fileName = name + ".txt";
	        fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");// 谷歌  
	        response.reset(); // 非常重要
	        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
	        response.setContentType("application/vnd.ms-excel;charset=utf-8");
	        response.setCharacterEncoding("UTF-8");

			while ((len = br.read(buf)) > 0){
	        	out.write(buf, 0, len);
	        }
	        fos.close();
	        br.close();
	        out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

猜你喜欢

转载自blog.csdn.net/weixin_40452506/article/details/105429722