jsp 上传
<form id="inputForm" name="inputForm" action="sys-notebook!save.action" method="post" target="_self" enctype="multipart/form-data">
<tr>
<td>备忘录附件上传:</td>
<td><input type="file" name="file" size="60"/></td>
</tr>
</form>
jsp下载或显示图片
<tr>
<td class="form_label">附件:</td>
<td colspan="3">
<!--<img src="sys-notebook!downLoadBlob.action?noteId=${noteId}"></img></td>-->
<a href="sys-notebook!downLoadBlob.action?noteId=${noteId}">${noteAtchName}</a></td>
</tr>
action要写的属性
private File file;
private String fileFileName;
set和get不能少
DAO层里面上传
/**
* 保存备忘录,处理Blob 增
*@param sysNotebook
*@param photo void
*/
public void saveSysNotebook(SysNotebook sysNotebook,File file,String fileFileName,String fileContentType){
FileInputStream in=null;
Blob blobData=null;
if(file!=null){
try {
in=new FileInputStream(file);
blobData=Hibernate.createBlob(in);
//实体注入属性值
sysNotebook.setNoteAtchCont(blobData);
String realFilePath = file.getAbsolutePath();//得到文件的绝对路径
sysNotebook.setNoteAtchAddr(realFilePath);
sysNotebook.setNoteAtchName(fileFileName);//文件名
this.save(sysNotebook);
} catch (IOException e) {
e.printStackTrace();
}
}else{
if(sysNotebook.getNoteId()!=null){
//不改就是数据库的图片
String hql="select s.noteAtchCont as noteAtchCont from SysNotebook s where noteId=?";
Query query=createQuery(hql);
query.setLong(0,sysNotebook.getNoteId());
Blob blob=(Blob) query.uniqueResult();
sysNotebook.setNoteAtchCont(blob);
this.save(sysNotebook);
}else{
this.save(sysNotebook);
}
}
}
下载(用猎豹浏览器会报)ClientAbortException: java.net.SocketException: Connection reset by peer: socket write error
Caused by: java.net.SocketException: Connection reset by peer: socket write error
/**
* 获取备忘录,数据库查找Blob,相当于下载(待)文件 ,查blob
*@param noteId void
*/
public void getNoteFileBlob(Long noteId){
BufferedInputStream input = null;//中间入的缓冲
InputStream in = null;//本地数据到内存
BufferedOutputStream out = null;//中间出的缓冲
// hql语句查询
String hql = "select noteAtchCont as noteAtchCont,noteAtchName as noteAtchName from SysNotebook where noteId=?";
Query hqlQuery =this.getSession().createQuery(hql).setResultTransformer(Transformers.aliasToBean(SysNotebook.class));
hqlQuery.setLong(0, noteId);
SysNotebook sysNotebook=(SysNotebook) hqlQuery.uniqueResult();
Blob blobfile=(Blob)sysNotebook.getNoteAtchCont();
String filename=sysNotebook.getNoteAtchName();
// 得到一个输出
HttpServletResponse res = ServletActionContext.getResponse();//------这是关键,响应
// 判断有没有照片
if (blobfile != null) {
try {
out = new BufferedOutputStream(res.getOutputStream());
in = blobfile.getBinaryStream();//----------------大字段对象得到二进制流。
input = new BufferedInputStream(in);
byte[] buffer = new byte[1024];
int count = 0;
String name = new String(filename.getBytes("UTF-8"),"ISO-8859-1");
res.setHeader("Content-Type", "application/x-download");
res.setHeader("Content-Disposition", "attachment; filename=\""+name+"\"");
while ((count = input.read(buffer)) != -1) {// 表示文件的末尾
out.write(buffer, 0, count);
out.flush();
}
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();//后用的
input.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}