1、bean
代码语言:javascript
复制package com.cntaiping.tpa.bean;
import java.sql.Blob;
public class AttachmentBean {
private Integer id;
private String finename;
private Long contentSize;
private String fileType;
//Java的Object类型来对应数据库的BLOB类型,后边将Object转化成Blob类型
private Object content;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFilename() {
return finename;
}
public void setFileName(String finename) {
this.finename= finename;
}
public Long getContentSize() {
return contentSize;
}
public void setContentSize(Long contentSize) {
this.contentSize = contentSize;
}
public String getFileType() {
return fileType;
}
public void setFileType(String fileType) {
this.fileType = fileType;
}
public Object getContent() {
return content;
}
public void setContent(Object content) {
this.content = content;
}
}
2、dao
代码语言:javascript
复制package com.cntaiping.tpa.dao;
import com.cntaiping.tpa.bean.AttachmentBean;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface AttachmentDao {
@Select("select ID,FILENAME,CONTENTSIZE,FILETYPE,CONTENT from attachment")
@Results({
@Result(column="ID",property="id"),
@Result(column="FILENAME",property="filename"),
@Result(column="CONTENTSIZE",property="contentSize"),
@Result(column="FILETYPE",property="fileType"),
@Result(column="CONTENT",property="content"),
})
List<AttachmentBean> getAttachmentList();
}
3、service
代码语言:javascript
复制package com.cntaiping.tpa.service.impl;
import com.cntaiping.tpa.bean.AttachmentBean;
import com.cntaiping.tpa.dao.AttachmentDao;
import com.cntaiping.tpa.dao.datasource.DataSource;
import com.cntaiping.tpa.service.AttachmentService;
import oracle.sql.BLOB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.io.*;
import java.sql.SQLException;
import java.util.List;
@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT)
@Service("attachmentService")
@DataSource("ORACLE")
public class AttachmentServiceImpl implements AttachmentService {
@Autowired
private AttachmentDao attachmentDao;
@Override
public List<AttachmentBean> getAttachmentList() {
return attachmentDao.getAttachmentList();
}
@Override
public int parseAttachmentList() throws SQLException {
int i=0;
BLOB blob=null;
List<AttachmentBean> list=attachmentDao.getAttachmentList();
for(AttachmentBean a:list){
blob=(BLOB)a.getContent();
if(blob!=null) {
i ;
writeNewFile(blob.getBinaryStream(), "D:\a\" a.getObjId() a.getName() "." a.getFileType());
}else{
System.out.println("空附件:" a.getName());
}
}
return 0;
}
private boolean writeNewFile(InputStream in,String savePath){
File file=new File(savePath);
OutputStream os=null;
try {
os= new BufferedOutputStream(new FileOutputStream(savePath));
int len = 0;
byte[] buffer = new byte[1024];
while ((len = in.read(buffer)) > 0) {
os.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
private boolean writeNewFile(byte[] data,String savePath){
File file=new File(savePath);
OutputStream os = null;
try {
os = new FileOutputStream(file);
os.write(data, 0, data.length);
} catch (Exception e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
}