spring+springMVC+hibernate 存取对象中有文件类型的方式
表结构
/*ID VARCHAR2(255) 标识
ASK_ID VARCHAR2(255) Y 提问人标识
ASK_TYPE_NAME VARCHAR2(255) Y 提问类型名称
ASK_CONTENT VARCHAR2(1000) Y 提问内容
ASK_TIME DATE Y 提问时间
ASK_STATUS VARCHAR2(2) Y 问题状态 1待反馈 2已反馈
ASK_TYPE_CODE VARCHAR2(255) Y 提问类型编号
ASK_RESOURCE_CODE VARCHAR2(255) Y 问题来源编号
ASK_RESOURCE_NAME VARCHAR2(255) Y 问题来源名称
ASK_ENCLOSURE BLOB 附件
ASK_ENCLOSURE_NAME VARCHAR2(255) 文件名
*/
实体类 注解配置表和对象的映射关系
import java.sql.Blob;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.format.annotation.DateTimeFormat;
@Entity
@Table(name = "WTGL_PROBLEM")
public class AskBo {
private String id; //标识
private String askId; // 提问人标识
private String askTypeCode; // 提问类型编号
private String askTypeName; // 提问类型名称
private String askcontent; // 提问内容
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
private Date askTime; // 提问时间
private String askStatus; // 问题状态 1待反馈 2已反馈
private String askResourceCode; // 问题来源编号
private String askResourceName; // 问题来源名称
private byte[] askEnclosure ; //附件
private String askEnclosureName; // 问题来源名称
public AskBo() {
this.askTime = new Date();
this.askStatus = "1";
}
public AskBo(String id, String askId, String askTypeCode,
String askTypeName, String askcontent, Date askTime,
String askStatus, String askResourceCode, String askResourceName,
byte[] askEnclosure) {
super();
this.id = id;
this.askId = askId;
this.askTypeCode = askTypeCode;
this.askTypeName = askTypeName;
this.askcontent = askcontent;
this.askTime = askTime;
this.askStatus = askStatus;
this.askResourceCode = askResourceCode;
this.askResourceName = askResourceName;
this.askEnclosure = askEnclosure;
}
@Id
@Column(name="id",unique = true ,nullable = false)
@GeneratedValue(generator = "idGenerator")
@GenericGenerator(name = "idGenerator" ,strategy = "uuid")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Column(name="ASK_ID")
public String getAskId() {
return askId;
}
public void setAskId(String askId) {
this.askId = askId;
}
@Column(name="ASK_TYPE_CODE")
public String getAskTypeCode() {
return askTypeCode;
}
public void setAskTypeCode(String askTypeCode) {
this.askTypeCode = askTypeCode;
}
@Column(name="ASK_TYPE_NAME")
public String getAskTypeName() {
return askTypeName;
}
public void setAskTypeName(String askTypeName) {
this.askTypeName = askTypeName;
}
@Column(name="ASK_CONTENT")
public String getAskcontent() {
return askcontent;
}
public void setAskcontent(String askcontent) {
this.askcontent = askcontent;
}
@Column(name="ASK_TIME")
public Date getAskTime() {
return askTime;
}
public void setAskTime(Date askTime) {
this.askTime = askTime;
}
@Column(name="ASK_STATUS")
public String getAskStatus() {
return askStatus;
}
public void setAskStatus(String askStatus) {
this.askStatus = askStatus;
}
@Column(name="ASK_RESOURCE_CODE")
public String getAskResourceCode() {
return askResourceCode;
}
public void setAskResourceCode(String askResourceCode) {
this.askResourceCode = askResourceCode;
}
@Column(name="ASK_RESOURCE_NAME")
public String getAskResourceName() {
return askResourceName;
}
public void setAskResourceName(String askResourceName) {
this.askResourceName = askResourceName;
}
@Lob
@Basic(fetch=FetchType.LAZY)
@Column(name="ASK_ENCLOSURE",columnDefinition="BLOB",nullable=true)
public byte[] getAskEnclosure() {
return askEnclosure;
}
public void setAskEnclosure(byte[] blob) {
this.askEnclosure = blob;
}
@Column(name="ASK_ENCLOSURE_NAME")
public String getAskEnclosureName() {
return askEnclosureName;
}
public void setAskEnclosureName(String askEnclosureName) {
this.askEnclosureName = askEnclosureName;
}
@Override
public String toString() {
return "AskBo [id=" + id + ", askId=" + askId + ", askTypeCode="
+ askTypeCode + ", askTypeName=" + askTypeName
+ ", askcontent=" + askcontent + ", askTime=" + askTime
+ ", askStatus=" + askStatus + ", askResourceCode="
+ askResourceCode + ", askResourceName=" + askResourceName
+ ", askEnclosure=" + askEnclosure + "]";
}
}
Controller
import gov.sso.client.UUID;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.hibernate.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.jcraft.jsch.Session;
import com.wonders.Bo.AnswerBo;
import com.wonders.Bo.AskBo;
import com.wonders.Vo.FormVo;
import com.wonders.Vo.PageBean;
import com.wonders.service.AskService;
@Controller
@RequestMapping("/ask")
public class AskController {
@Autowired
AskService askServiceImpl;
/**
*文件上传
*/
@RequestMapping("/save")
@ResponseBody
public Object saveProblem(MultipartFile upFile ,String pType,String pTypeName,String newContent) {
AskBo askBo=new AskBo();
//用户id
askBo.setAskId("102");
//获得当前系统
//
askBo.setAskcontent(newContent);
askBo.setAskTypeName(pTypeName);
askBo.setAskTypeCode(pType);
if(!upFile.isEmpty()){
String flieName=upFile.getOriginalFilename();
//创建临时文件
File tempFile = null;
try {
tempFile = File.createTempFile(flieName.substring(0,flieName.lastIndexOf(".")-1)+String.valueOf(System.currentTimeMillis()), flieName.substring(flieName.lastIndexOf(".")).toString());
//上传文件
upFile.transferTo(tempFile);
} catch (IOException e1) {
return "文件不符合请重新上传";
}
//转为byte[]
byte[] byteFile = fileConvertToByteArray(tempFile);
askBo.setAskEnclosure(byteFile);
askBo.setAskEnclosureName(tempFile.getName());
}
//执行保存
AskBo save = askServiceImpl.save(askBo);
return "true";
/*return "redirect:../ask.jsp";*/
}
/**
* 下载附件
* @param request
* @return
*/
@RequestMapping("/downLoadFile")
public ResponseEntity<byte[]> downLoadFile(HttpServletRequest request){
//获得问题id
String id = request.getParameter("id");
System.out.println("问题"+id);
AskBo askBo = askServiceImpl.getProblemById(id);
//文件
byte[] fileByte = askBo.getAskEnclosure();
//文件名
String fileName = askBo.getAskEnclosureName();
HttpHeaders headers=new HttpHeaders();
headers.add("Content-Disposition", "attchement;filename="+fileName);
HttpStatus statusCode=HttpStatus.OK;
ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(fileByte,headers,statusCode);
return entity;
}
/**
* 把一个文件转化为byte字节数组。
* @return
*/
private byte[] fileConvertToByteArray(File file) {
byte[] data = null;
try {
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[1024];
while ((len = fis.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
data = baos.toByteArray();
fis.close();
baos.close();
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
}
文件下载js
//创建form 提交请求 提交后 清除
var url="answer/downLoadFile.action";
var form=$("<form method=post action='"+url+"' > <input type='hidden' name='id' id='id' value='"+id+"'/></from>");
form.submit();
form.remove();