一、File类

import java.io.*;public class NumberFormatDmeo{    public static void main(String[] args)throws Exception{        File f=new File("f:"+File.separator+"test.txt");//没有该文件的话会自动创造        OutputStream out=new FileOutputStream(f);//输出        String str="Hello World!!!";        byte[] b=str.getBytes();   //把str的内容保存到byte[]数组中        for(int i=0;iout.write(b[i]);    //用OutputStream把byte[]的内容输出。        }    }}

二、字节流的输入输出
InputStream:字节流的输入(把文件从外面输入到程序)
OutputStream:字节流的输出(从程序输出到外面的文件)
三、字符流的输出输入
字符输入流:Writer
字符输入流:Reader
四、字符流字节流相互的转换
OutputStreamWriter 字符流–>字节流
InputStreamReader 字符流–>字节流
Android之JAVASe基础篇-面向对象-IO(九)_第1张图片
五、运用例子:
1.文件(图片为例子)上传
jsp页面代码:

"form1" action="add/submitQuestion" method="post" enctype="multipart/form-data"> type="file" name="files" value=""/> type="file" name="files" value=""/> type="file" name="files" value=""/>

后台Controller层代码:

@RequestMapping("/add/submitQuestion")public String submitQuestion(@RequestParam("files") CommonsMultipartFile[] files) throws Exception{        String picUrl = "";        for(int i=0;iif(!"".equals( files[i].getOriginalFilename() )){                      //拿到上传文件的输入流                    InputStream in=(InputStream)files[i].getInputStream();                    String fileName =files[i].getOriginalFilename();                    //获取图片最后三位                    String subStr = fileName.substring(fileName.length() -3, fileName.length());                    //把输入流转换为二进制                    byte[] b = submit.toByte(in);                    ....代码未完,关键代码已经列出。

还可参考:完整的代码参考
2.图片显示
页面直接用标签
jsp页面:

<img src="/showPic?info_files='+arr[i]+'"/>

后台代码:

@RequestMapping("/showPic")public void showPic(@RequestParam("info_files") String info_files,HttpServletRequest request,HttpServletResponse response) throws Exception{        if(info_files != null && !"".equals(info_files)){            .....            //获取图片输入流,一定要获取输入流InputStream后放到byte[]数组使用OutputStream输出            List list =  pic.showPic(info_files);            for(int i=0;i"image/*");                ServletOutputStream os=response.getOutputStream();                int count = 0;                byte[] buffer = new byte[fis.available()];                while ( (count = fis.read(buffer)) != -1 ){                     os.write(buffer, 0, count);                     os.flush();                    }                os.flush();                os.close();            }                                   }

更多相关文章

  1. Android很有用的代码片段
  2. android上传图片到服务器,求服务器那边和android的Activity的完整
  3. android 源代码研究之----frameworks-----status bar 状态栏
  4. Android 代码实现重启
  5. 【Arcgis android】 离线编辑实现及一些代码段
  6. Android 实现扫雷小游戏实例代码
  7. android代码分析,及Terminal使用
  8. Android应用程序绑定服务(bindService)的过程源代码分析(2)

随机推荐

  1. 1.vue常用术语 2. 样式与事件绑定 3. 列
  2. 【视频版】最新版PyCharm 2021.3.3 激活
  3. JS框架 -(一)fetch async await 模块 npm
  4. 实现鼠标悬停时自动停止播放离开时又自动
  5. vue常用术语、样式与事件绑定 和 列表渲
  6. 1. 实例演示fetch api, async,await的使
  7. 从淘宝进货卖到亚马逊,能做到利润可观吗?
  8. 【视频版】Goland 2022.1 最新正版激活方
  9. 【视频版】phpstorm 2021.3.3 最新正版激
  10. android有用的知识