关于上传音像等文件之后以及转换成字节流进行入库

前端页面

1、导包

<link rel="styleSheet" type="text/css"
href="<c:url value='/fileupload/fileupload.css'/>" />
<script type="text/javascript"
src="<c:url value='/fileupload/fileupload.js' />"></script>
<script type="text/javascript">

2、上传文件按钮

<th>
上传文件:
</th>
<td>
<input type="file" class="input_field" name="uploadfile"
id="uploadfile" />
</td>

3、上传文件部件初始化要在$(function(){})里面实现

function initFileInputs() {
$(document).fileInput( {
clickImageId : "addFileImage",
appendDivId : "fileUploadDivContainer",
cloneDivId : "fileUploadDiv",
showNumber : 2
});
}

后台逻辑

1、用FileUploadMultipleDateUtil 获取实体,将上传的文件转成字节流

@RequestMapping(params = "action=doAdd")

public void addData(HttpServletRequest request, HttpServletResponse response) {
MessageVo vo = new MessageVo();
vo.setCode(Constants.ERROR_CODE);
TB0005_SNIMDT obj = null;
try {
FileUploadMultipleDateUtil fumdu = new FileUploadMultipleDateUtil(
FileUploadMultipleDateUtil.MAP_CONVERT_TYPE);
fumdu.registerDatePattern("flcltm", "yyyy-MM-dd");
obj = fumdu
.uploadFile(request, TB0005_SNIMDT.class, "upload\\file");
List<FileDomain> list = null;
if (obj == null || (list = obj.getFileList()) == null
|| list.size() == 0) {
vo.setMsg("上传文件失败,请重新上传。");
} else {
FileDomain fd = list.get(0);// 取第一个文件
obj.setFltp(fd.getFileExtName());
String filePath = fumdu.getPRO_ABSOULTE_PATH().concat(
fd.getRelativePath()).concat(
System.getProperty("file.separator")).concat(
fd.getGenFileName());
java.io.File file = new java.io.File(filePath);
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(
fileInputStream);
byte[] byteArray = IOUtils.toByteArray(bufferedInputStream);
obj.setBrfl(byteArray);
}
} catch (Exception e) {
logger.warn("未上传文件...", e);
}
try {
tb0005_snimdtService.insert(obj);
vo.setMsg("保存成功!");
vo.setCode(Constants.RIGHT_CODE);
} catch (Exception e) {
vo.setMsg("保存失败。");
logger.error("保存出现异常:", e);
}
super.forwardAjax(response, JSONObject.fromObject(vo).toString(),
WebUtils.CONTENTTYPE_TEXTJSON, WebUtils.CONTENT_CHARSET_UTF8);

}

文件下载

1、前台实现方法

function downloadFile(snpccd){
var url = "<c:url value='/snimdt.do?action=downloadFile&snpccd='/>"+snpccd;
window.location.href = url;
}

2、后台逻辑

/**
* @Title: downloadFile
* @Description: 文件下载
* @param
* @param model
* @param
* @param response
* @param
* @param snpccd
* @param
* @throws Exception
*             设定文件
* @return void 返回类型
* @author pigZZ
* @throws
*/
@RequestMapping(params = "action=downloadFile")
public void downloadFile(Model model, HttpServletResponse response,
@RequestParam(required = false, value = "snpccd")
String snpccd) throws Exception {
TB0005_SNIMDT ts = tb0005_snimdtService.getbySnpccd(snpccd);
response.setContentType(ts.getFltp());
response.setHeader("Content-Disposition", "attachment; filename="
+ ts.getFlnm() + "." + ts.getFltp());
OutputStream outputStream = response.getOutputStream();
byte[] buffer = ts.getBrfl();
response.setContentLength(buffer.length);
if (null != buffer) {
outputStream.write(buffer, 0, buffer.length);
}
outputStream.flush();
outputStream.close();
outputStream = null;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值