分享一下FastDFS的简单上手的demo,挺简单的.其中文件上传流程大致如下:
controller里的上传方法:
- @RequestMapping("/fastdfs/upload")
- @ResponseBody
- public String uploadCreditorInfo(@RequestParam("id") Integer id, @RequestParam("fileName") MultipartFile file){
- int status = -1;
- //文件扩展名
- //originalFilename得到的文件名:aaa.txt
- String originalFilename = file.getOriginalFilename();
- String fileExtName = originalFilename.substring(originalFilename.indexOf(".")+1);
- try {
- //将文件转换为字节数组
- byte[] fileBatys = file.getBytes();
- //调用service上传方法
- status = creditorInfoService.uploadCreditorInfo(id,fileBatys,fileExtName);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return "<script>window.parent.uploadOK("+status+")</script>";
- }
- public int uploadCreditorInfo(Integer id,byte[] fileBatys, String exName) {
- int retCode = 10000;
- String[] arry=null;
- //上传
- if ( exName != null && exName.length()>0) {
- arry = FastDFSClient.uploadFile(fileBatys,exName);
- //更新数据库信息
- String contracturl = "http://192.168.174.129/" + arry[0] + "/" + arry[1];
- CreditorInfo creditorInfo = new CreditorInfo();
- creditorInfo.setContracturl(contracturl);
- creditorInfo.setGroups(arry[0]);
- creditorInfo.setPath(arry[1]);
- creditorInfo.setId(id);
- retCode = creditorInfoMapper.updateByPrimaryKeySelective(creditorInfo);
- }
- return retCode;
- }
- /**
- * 文件上传
- *
- * @param fileBytes
- * @param fileExtName
- * @return
- */
- public static String[] uploadFile (byte[] fileBytes, String fileExtName) {
- TrackerServer trackerServer = null;
- StorageServer storageServer = null;
- String[] array = null;
- try {
- //1.初始化fastdfs的连接信息配置
- ClientGlobal.init("fastdfs.properties");
- //2.创建一个Tracker的客户端对象
- TrackerClient trackerClient = new TrackerClient();
- //3.获得一个Tracker server对象
- trackerServer = trackerClient.getConnection();
- //4.获得一个Storage server对象
- storageServer = trackerClient.getStoreStorage(trackerServer);
- //5.创建一个Storage客户端对象
- StorageClient storageClient = new StorageClient(trackerServer, storageServer);
- //6.通过storageClient客户端对象,就可以去操作fastdfs文件系统了
- //文件上传
- array = storageClient.upload_file(fileBytes, fileExtName, null);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (MyException e) {
- e.printStackTrace();
- } finally {
- try {
- if (null != trackerServer) {
- trackerServer.close();
- }
- if (null != storageServer) {
- storageServer.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return array;
- }
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>FastDFS---Demo</title>
- <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css">
- <script src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
- <script src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>
- </head>
- <body style="margin: 50px;">
- <form action="${pageContext.request.contextPath}/fastdfs/upload" class="form-horizontal" role="form" method="post" enctype="multipart/form-data" target="uploadFrame">
- <div class="form-group">
- <label class="col-sm-2 control-label">借款人真实姓名</label>
- <div class="col-sm-10">
- <input type="text" class="form-control" id="realname" name="realname" value="${creditorInfo.realname}">
- </div>
- </div>
- <div class="form-group">
- <label class="col-sm-2 control-label">借款人手机号</label>
- <div class="col-sm-10">
- <input type="text" class="form-control" id="phone" name="phone" value="${creditorInfo.phone}">
- </div>
- </div>
- <div class="form-group">
- <label class="col-sm-2 control-label">借款人身份证</label>
- <div class="col-sm-10">
- <input type="text" class="form-control" id="idCard" name="idCard" value="${creditorInfo.idCard}">
- </div>
- </div>
- <div class="form-group">
- <label class="col-sm-2 control-label">借款人性别</label>
- <div class="col-sm-10">
- <input type="text" class="form-control" id="sex" name="sex" value="${creditorInfo.sex==1?"男":"女"}">
- </div>
- </div>
- <div class="form-group">
- <label class="col-sm-2 control-label">借款人地址</label>
- <div class="col-sm-10">
- <input type="text" class="form-control" id="address" name="address" value="${creditorInfo.address}">
- </div>
- </div>
- <div class="form-group">
- <label class="col-sm-2 control-label">上传合同</label>
- <div class="col-sm-10">
- <input type="file" class="form-control" id="fileName" name="fileName">
- </div>
- </div>
- <div class="form-group">
- <div class="col-sm-offset-2 col-sm-10">
- <input type="hidden" class="form-control" id="id" name="id" value="${creditorInfo.id}">
- <button type="submit" class="btn btn-default">提 交</button>
- </div>
- </div>
- <%--页面不刷新上传文件,借鉴一个iframe--%>
- <iframe id="uploadFrame" name="uploadFrame" style="display: none;"></iframe>
- </form>
- <%--上传后结果返回页面展示--%>
- <script type="text/javascript">
- function uploadOK(status) {
- alert(status)
- if (status == 1) {
- //文件上传成功 layer弹层
- layer.confirm('文件上传成功,是否需要跳转到列表页?', function(index){
- //do something
- window.location.href="${pageContext.request.contextPath}/fastdfs/index"
- layer.close(index);
- });
- } else {
- //文件上传失败
- layer.alert("Sorry,文件上传失败了")
- }
- }
- </script>
- <script src="${pageContext.request.contextPath}/layer/layer.js"></script>
- </body>
- </body>
- </html>
- <iframe id="uploadFrame" name="uploadFrame" style="display: none;"></iframe>
这段代码需要同表格一起提交,它与表格头部信息其实是一一对应的,就是表单头信息里的target="uploadFrame".如果编写时没有将下面这段代码加到表单里一起提交,那么页面是跳转不了的,.
- <form action="${pageContext.request.contextPath}/fastdfs/upload" class="form-horizontal" role="form" method="post" enctype="multipart/form-data" target="uploadFrame">