file-s-d

本文详细介绍了如何在JSP中实现文件上传,并通过URL下载或显示图片。包括上传时处理Blob数据的方法,以及从数据库获取并下载Blob数据的过程。重点解决了下载过程中可能遇到的异常情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
				}
			}	
		}
	}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值