數據庫存儲文件字段用BLOB類型,文件以二進制存入數據庫,數據庫爲oracle
採用服務端、客戶端分離,可根據實際情況修改,主要部分已標黃。
jsp
<div class="col-sm-12" >
<label for="abc952" class="col-xs-2 "><span>*</span>上傳附件:</label>
<div class="col-xs-12 col-md-10" id="sub_file">
<input id="abz001" name="abz001" value="" type="hidden" class="form-control" />
<input id="fileOrImg" name="fileOrImg" type="file" οnchange="catchAbz001()"
multiple class="file-loading" />
</div>
</div>
<!-- form裏按鈕區 -->
<div class="modal-footer">
<button id="savefile" type="button" class="unified-bigbotton-blue">保存</button>
<button type="button" class="unified-bigbotton-gray" οnclick="fileClear()">清空</button>
<button type="button" class="unified-bigbotton-gray" data-dismiss="modal">關閉</button>
</div>
js
/**
* 材料添加
*/
$("#savefile").on('click',function (obj) {
$("#abz001").val(abz001);
var file = $("#fileOrImg").val();
var abz021 = getFileName(file);
function getFileName(o) {
var pos = o.lastIndexOf("\\");
return o.substring(pos + 1);
}
if (abz021 == null || abz021 == "") {
doAlert("請添加附件");
return false;
} else {
$('#add_file').ajaxSubmit({
beforeSerialize : function() {
serializeForm();
},
type : "post",
url : "./lrs/hgapply/addFile",
dataType : "json",
success : function(data) {
$("#savefile").attr("disabled", false);
if (data.code == '1') {
doAlert(data.msg);
fileClear();
$("#addFile").modal('hide');
selFileList();
}
},
error : function(data) {
$("#savefile").attr("disabled", false);
return false;
}
});
}
});
/**
* 附件表單序列化
*/
var g_file = [];
function serializeForm() {
}
conctroller
/**
* 材料添加
* @param jsonBh12
* @return
* @throws Exception
*/
@RequestMapping(value = "/addFile", method = RequestMethod.POST)
@ResponseBody
public String addFile(HttpServletRequest request) throws Exception {
log.info("材料添加");
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
/** 處理上傳圖片 begin **/
List<BH12> fileList = new ArrayList<BH12>();
List<MultipartFile> multipartFiles = multipartRequest.getFiles("fileOrImg");
for (MultipartFile multipartFile : multipartFiles) {
BH12 bh12 = new BH12();
String abz001 = multipartRequest.getParameter("abz001");
bh12.setAbz001(Long.valueOf(abz001));
String originalFilename = multipartFile.getOriginalFilename();// 原始文件名稱
//String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));// 文件後綴名
if (!this.checkFileType(suffix)) {
return respSuccssMsg("[" + originalFilename + "]文件格式錯誤");
}
if (!this.checkFileSize(multipartFile)) {
return respSuccssMsg("[" + originalFilename + "]文件大小("
+ multipartFile.getSize()/1024/1024 + " MB)超出");
}
bh12.setAbz021(originalFilename);// 獲取文件名
byte[] abz022 = multipartFile.getBytes();// 獲取文件二進制數據
bh12.setAbz022(abz022);
fileList.add(bh12);
}
/** 處理上傳圖片 end */
return WSClient.call(OS.WS_LRS_HonestGradeApplyService, "addFile", new String[] { JacksonUtil.toString( fileList ) });
}
/**
* 校驗文件類型
*
* @param suffix
* @return
*/
private boolean checkFileType(String suffix) {
String s = suffix.toLowerCase();
for (String type : IMAGE_TYPE) {
if (type.equals(s)) {
return true;
}
}
return false;
}
private static final String[] IMAGE_TYPE = { ".jpg", ".jpeg", ".png",
".gif", ".pdf", ".doc", ".docx", ".txt", ".xls", ".xlsx", ".rar",
".zip" };
/**
* 校驗文件大小
*
* @param multipartFile
* @return
*/
private boolean checkFileSize(MultipartFile multipartFile) {
// System.out.println(multipartFile.getSize());
if (multipartFile.getSize() > IMAGE_MAX_SIZE) {
return false;
}
return true;
}
/**
* 單文件最大上傳大小 (B)
*/
private static final long IMAGE_MAX_SIZE = 40 * 1024 * 1024L;// 40MB
/**
* 返回json數據(默認返回成功數據,code="1",data=null)
*
* @param msg
* 提示信息
* @return
*/
private String respSuccssMsg(String msg) {
return respResult(OS.SUCCESS, msg, null);
}
/**
* 返回json數據
*
* @param code
* 狀態碼
* @param msg
* 提示信息
* @param data
* 數據
* @return
*/
private String respResult(String code, String msg, Object data) {
return respResult(new Ret(code, msg, data));
}
/**
* 返回json數據
*
* @param ret
* @return
*/
private String respResult(Ret ret) {
return JsonUtil.toJson(ret);
}
Ret.java
import java.io.Serializable;
public class Ret implements Serializable{
/**
*
*/
private static final long serialVersionUID = -247432687409474017L;
private Integer draw; //必要
private Integer total; //必要,總記錄條數
private String code;
private String msg;
private Object data;
public Ret(String code, String msg, Object data) {
super();
this.code = code;
this.msg = msg;
this.data = data;
}
public Ret(String code, String msg, Integer draw, Integer total,
Object data) {
super();
this.code = code;
this.msg = msg;
this.draw = draw;
this.total = total;
this.data = data;
}
public Integer getDraw() {
return draw;
}
public void setDraw(Integer draw) {
this.draw = draw;
}
public Integer getRecordsTotal() {
return total;
}
public void setRecordsTotal(Integer recordsTotal) {
this.total = recordsTotal;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
service
/**
* 材料批量上傳
* @param jsonBh12
* @return
*/
public String addFile(String jsonBh12List){
List<BH12> fileList = new ArrayList<BH12>();
try {
fileList = (List<BH12>) JacksonUtil.toList(jsonBh12List, ArrayList.class, BH12.class);
} catch (Exception e) {
log.error("申報材料數據轉換出錯", e);
return respErrorMsg("申報材料數據轉換出錯");
}
try {
for (BH12 newbh12 : fileList) {
newbh12.setAbz020(new Long(comService.getSeq("SEQ_ES_BH12_ABZ020")));
bh12Mapper.addFile(newbh12);
}
return respSuccssMsg("材料添加成功!");
} catch (Exception e) {
return respErrorMsg("材料添加失敗");
}
}
mapper
/**
* 申報材料添加
* @param record
* @return
*/
int addFile(BH12 record);
mapper.XML
<!-- 申報材料添加 -->
<insert id="addFile" parameterType="BH12">
insert into BH12 (ABZ020, ABZ001, ABZ021,
ABZ022)
values (#{abz020,jdbcType=DECIMAL}, #{abz001,jdbcType=DECIMAL},
#{abz021,jdbcType=VARCHAR},
#{abz022,jdbcType=BLOB})
</insert>
實體類
/**
* 附件ID
*/
private Long abz020;
/**
* 關聯ID
*/
private Long abz001;
/**
* 附件名稱
*/
private String abz021;
/**
* 附件內容
*/
private byte[] abz022;