`
tangqi609567707
  • 浏览: 34639 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

java 读取文件并存入数据库

    博客分类:
  • JAVA
阅读更多

工作中发现有一个功能不好使,虽然公司框架有上传功能,但是不适用,所以自己写了一个简单的样例,部分代码如下:

       

           //获取文件minetype使用
            FileNameMap fileNameMap = URLConnection.getFileNameMap(); 
            File file=null;
            InputStream is = null;
            //多张图片,fjpsth是图片路径
            for(int i=0;i<fjpsth.length;i++){
                file = new File(fjpsth[i]);
                //获取图片名称
                String name = file.getName();
                //获取图片minetype
                String type = fileNameMap.getContentTypeFor(fjpsth[i]); 
                //把文件转换成字节数组
                is = new FileInputStream(file);
                long length = file.length();
                if (length > Integer.MAX_VALUE) {
                     throw new IOException("File is to large "+file.getName());
                }
                byte[] bytes = new byte[(int)length];
                int offset = 0;
                int numRead = 0;
                while (offset < bytes.length
                   && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
                  offset += numRead;
                }
               if (offset < bytes.length) {
                   throw new IOException("Could not completely read file "+file.getName());
               }
               //插入数据库
               …………
            }
            is.close();

 

0
7
分享到:
评论
2 楼 tangqi609567707 2014-11-17  
cwqcwqmax9 写道
没说明白。没说清楚  。

谢谢你的评论,以后我会写详细明白。
1 楼 cwqcwqmax9 2014-11-17  
没说明白。没说清楚  。

相关推荐

Global site tag (gtag.js) - Google Analytics