使用jfinal读取oracle的blob二进制图片数据或指定路径的图片

ohaozy 发布于 2014/03/03 15:30
阅读 1K+
收藏 2

在项目中用到了读取指定文件夹或者共享的其它机器的图片文件夹数据,或者读取二进制blob字段来展现图片,参考其它功能写了个简单的ImgRender:

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


import com.jfinal.render.Render;
import com.jfinal.render.RenderException;
import com.jfinal.render.RenderFactory;


public class ImgRender extends Render{

private static final long serialVersionUID = 4293616220202691369L;
private File file;
private String fileName;
private byte[] blob;

public ImgRender(File file) {
this.file = file;
}

public ImgRender(String fileName) {
this.fileName = fileName;
}
public ImgRender(byte[] blob) {
this.blob = blob;
}
public static ImgRender render(byte[] blob){
ImgRender render = new ImgRender(blob);
return render;
}
public static ImgRender render(String fileName){
ImgRender render = new ImgRender(fileName);
return render;
}
public static ImgRender render(File file){
ImgRender render = new ImgRender(file);
return render;
}

public void render() {
if (file == null && fileName != null) {
file = new File(fileName);
}

if ((file == null || !file.isFile() || file.length() > Integer.MAX_VALUE)&&blob==null) {
RenderFactory.me().getErrorRender(404).setContext(request, response).render();
return ;
        }

        String contentType = "image/jpeg";//servletContext.getMimeType(file.getName());
        
        response.setContentType(contentType);
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            if(file!=null){
            inputStream = new BufferedInputStream(new FileInputStream(file));
           byte[] buffer = new byte[1024];
           for (int n = -1; (n = inputStream.read(buffer)) != -1;) {
               outputStream.write(buffer, 0, n);
           }
            }else if(blob!=null){
                 outputStream.write(blob);
            }
            outputStream.flush();
        }
        catch (Exception e) {
        throw new RenderException(e);
        }
        finally {
            if (inputStream != null) {
                try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
            }
            if (outputStream != null) {
            try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
            }
        }
}
}

用法比较简单:

读取图片路径:render(ImgRender.render("d:/test/1.jpg"));

读取文件:render(ImgRender.render(new File("d:/test/1.jpg")));

读取二进制:

int id = this.getParaToInt("id");
Record r = Db.findFirst("select plateImgData from running_arch where id=?",id);
byte[] blob = r.get("plateImgData");//这个blob字段读出来的就是byte数组了,其它数据库未测试
render(ImgRender.render(blob));

这个Render类只实现最基本的读取jpg格式的功能,请同仁们完善。

加载中
0
木川瓦兹
木川瓦兹
楼主有没有试试图片二进制写入呢?
0
ohaozy
ohaozy

引用来自“飞翔的猴子”的答案

楼主有没有试试图片二进制写入呢?
二进制写入数据库非常简单,这是大概的代码片段:
直接set("字段",返回的byte[])
File file = new File(filename);
long len = file.length();
byte[] bytes = new byte[(int) len];
BufferedInputStream bufferedInputStream = new BufferedInputStream(
new FileInputStream(file));
int r = bufferedInputStream.read(bytes);
if (r != len){
bufferedInputStream.close();
throw new IOException("读取文件不正确");
}
bufferedInputStream.close();
return bytes;
木川瓦兹
木川瓦兹
嗯 我这样写进去了。但是我的这个是个修改的项目,原项目使用Hibernate做的,Hibernate写进去的二进制好像和这个不一样
0
写下带不走的风
写下带不走的风
package com.wasteful.ext.render;


import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


import com.jfinal.render.Render;
import com.jfinal.render.RenderException;
import com.jfinal.render.RenderFactory;


public class BlobRender extends Render {


private static final long serialVersionUID = 4293616220202691369L;
private File file;
private String fileName; //完整的文件路径
private byte[] blob;
private String name; //文件名称 a.jpg

public BlobRender(){

}


public BlobRender(File file,String name) {
this.file = file;
this.name = name;
}


public BlobRender(String fileName) {
this.fileName = fileName;
}


public BlobRender(byte[] blob,String name) {
this.blob = blob;
this.name = name;
}


public static BlobRender render(byte[] blob,String name) {
BlobRender render = new BlobRender(blob,name);
return render;
}


public static BlobRender render(String fileName) {
BlobRender render = new BlobRender(fileName);
return render;
}


public static BlobRender render(File file,String name) {
BlobRender render = new BlobRender(file,name);
return render;
}


public void render() {
if (file == null && fileName != null) {
file = new File(fileName);
}


if ((file == null || !file.isFile() || file.length() > Integer.MAX_VALUE)
&& blob == null) {
RenderFactory.me().getErrorRender(404)
.setContext(request, response).render();
return;
}


response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment;filename=\""+name+"\"");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
InputStream inputStream = null;
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
if (file != null) {
inputStream = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024];
for (int n = -1; (n = inputStream.read(buffer)) != -1;) {
outputStream.write(buffer, 0, n);
}
} else if (blob != null) {
outputStream.write(blob);
}
outputStream.flush();
} catch (Exception e) {
throw new RenderException(e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
0
写下带不走的风
写下带不走的风
主要是对你的方法做了些改动,目前有blob的需求,所以主要针对Blob类型做的修改。
返回顶部
顶部