current position:Home>JAVA reads the content of the text file and returns it to the front end in the form of a stream

JAVA reads the content of the text file and returns it to the front end in the form of a stream

2022-09-23 03:27:53Big boss T

JAVARead the text file content back to the front end as a stream

This method putsController

		ServletOutputStream out = null;
		ByteArrayOutputStream baos = null;
		try {
    
			File file=new File(filename);
			InputStream inStream=new FileInputStream(file);
			byte[] buffer = new byte[1024];
			int len;
			baos = new ByteArrayOutputStream();
			while ((len = inStream.read(buffer)) != -1) {
    
				baos.write(buffer, 0, len);
			}
			out = response.getOutputStream();
			out.write(baos.toByteArray());
		} catch (Exception e) {
    
			e.printStackTrace();
		} finally {
    
			baos.flush();
			baos.close();
			out.flush();
			out.close();
		}

copyright notice
author[Big boss T],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/266/202209230313271748.html

Random recommended