后端:Java字节流保存文件
不管使用什么框架,客户端发送Http---Post 请求之后,
- 服务器端获取网络字节流输入管道,用于读取文件字节流
- 服务器端创建本地字节流输出管道,用于写入文件字节流
- 定义缓存区,每次读满之后,输出流将缓存区中的内容写入到文件
- 读取完毕之后,关闭输入流管道
- 刷新输出流管道,写入文件
- 关闭输出流
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Test { public static void main(String[] args) { try { InputStream is = request.getInputStream(); File filedir = new File("服务器绝对路径文件夹常量"); if (!filedir.exists()) filedir.mkdirs(); File file = new File(filedir, "文件名.后缀名"); OutputStream os = new FileOutputStream(file); byte[] byteStr = new byte[1024]; int len = 0; while ((len = is.read(byteStr)) > 0) { os.write(byteStr,0,len); } is.close(); os.flush(); os.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
http://www.cnblogs.com/makexu/

浙公网安备 33010602011771号