1.数据库炫耀两张表,SYS_ATTACHMENT(存放附件内容)和SYS_ATTACHMENTBUSINESSRELATION(存放附件和业务内容相关)。定义如下:
create table SYS_ATTACHMENT
(
eventid VARCHAR2(50),
filename VARCHAR2(50),
filetype VARCHAR2(50),
filesize NUMBER(12),
content BLOB,
filedescription VARCHAR2(200),
fileurl VARCHAR2(500),
createby VARCHAR2(50),
createddate DATE,
lastmodifyby VARCHAR2(50),
lastmodifydate DATE,
status NUMBER(4)
)
create table SYS_ATTACHMENTBUSINESSRELATION
(
attachmentid VARCHAR2(50),
businessdateid VARCHAR2(50),
businessdatetable VARCHAR2(50)
)
2.附件下载,附件内容存在Blob字段内。
主要方法:
/**
*
*@User :Test
*@date :2014-6-4 上午10:18:55
*@return :void
*@userFor :用于附件下载
*/
public void downloadAttachement(HttpServletRequest request, HttpServletResponse response)
{
//得到业务id
String eventid = request.getParameter("eventid");
Attachment attachmentEntity = xxService.getAttachement(eventid);
//获得附件内容Blob
Blob blob = attachmentEntity.getcontent();
try
{
InputStream inputstream = null;
byte filedata[] = null ;
if (blob != null)
{
inputstream = blob.getBinaryStream();
//获得字节数组
filedata = new byte[inputstream.available()];
}
String fileNameMid = "";
//处理中文乱码问题
fileNameMid = URLEncoder.encode(attachmentEntity.getfilename(), "utf-8");
//把+号转空格 %20是空格意思
fileNameMid = fileNameMid.replace("+", "%20");
//用于页面跳出下载提示
response.setHeader("Content-Disposition", (new StringBuilder("attachment;filename=")).append(fileNameMid).toString());
OutputStream outS = response.getOutputStream();
//下载到本地
outS.write(filedata);
inputstream.close();
response.getOutputStream().flush();
response.getOutputStream().close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
3.附件上传
采用的附件上传插件是ServletFileUpload,jar包是commons-fileupload-1.2.2.jar
Spring MVC对应的Controller方法是
public Map<String, Object> saveWaterSoil(MultipartHttpServletRequest request, xx entity)
/**
*
*@User :Test
*@date :2014-6-4 上午11:15:59
*@return :List<AttachmentBo>
*@userFor :从MultipartHttpServletRequest中获得附件集合
*/
public List<AttachmentBo> getAttachmentBoList(MultipartHttpServletRequest request)
{
List<AttachmentBo> attachmentBoList = new ArrayList<AttachmentBo>();
/**
* 为该请求创建一个DiskFileItemFactory对象,
* 通过它来解析请求。执行解析后,所有的表单项目都
* 保存在一个List中。
*/
DiskFileItemFactory factory = new DiskFileItemFactory();
//设置缓冲区
factory.setSizeThreshold(102400000);
ServletFileUpload upload = new ServletFileUpload(factory);
//设置附件上传大小
upload.setSizeMax(102400000);
upload.setHeaderEncoding("UTF-8");
HttpSession session = request.getSession();
String username = (String) session.getAttribute("user");
if (!upload.isMultipartContent(request))
{
return null;
}
try
{
Map<String, MultipartFile> multipartFileMap=request.getFileMap();
Set<String> keySet=multipartFileMap.keySet();
for (String key : keySet) {
AttachmentBo m_AttachmentBo = new AttachmentBo();
MultipartFile multipartFile=multipartFileMap.get(key);
String fileName=multipartFile.getOriginalFilename();
m_AttachmentBo.setfilesize(Integer.parseInt(multipartFile.getSize() + ""));
//获得附件内容
m_AttachmentBo.setcontent(Hibernate.createBlob(multipartFile.getBytes()));
m_AttachmentBo.setcreateby(username);
m_AttachmentBo.setlastmodifyby(username);
m_AttachmentBo.setfilename(fileName);
m_AttachmentBo.setfilesize(Integer.parseInt(((Long)multipartFile.getSize()).toString()));
String desc=request.getParameter(key.concat("desc"));
m_AttachmentBo.setfiledescription(desc);
attachmentBoList.add(m_AttachmentBo);
}
return attachmentBoList;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}