FastDFs分布式文件系统配置

分享一下FastDFS的简单上手的demo,挺简单的.其中文件上传流程大致如下:


controller里的上传方法:

[java]  view plain  copy
  1. @RequestMapping("/fastdfs/upload")  
  2.    @ResponseBody  
  3.    public String uploadCreditorInfo(@RequestParam("id") Integer id, @RequestParam("fileName") MultipartFile file){  
  4.        int status = -1;  
  5.   
  6.        //文件扩展名  
  7.        //originalFilename得到的文件名:aaa.txt  
  8.        String originalFilename = file.getOriginalFilename();  
  9.        String fileExtName = originalFilename.substring(originalFilename.indexOf(".")+1);  
  10.         
  11.        try {  
  12.            //将文件转换为字节数组  
  13.            byte[] fileBatys  = file.getBytes();  
  14.            //调用service上传方法  
  15.            status =  creditorInfoService.uploadCreditorInfo(id,fileBatys,fileExtName);  
  16.        } catch (IOException e) {  
  17.            e.printStackTrace();  
  18.        }  
  19.   
  20.   
  21.        return  "<script>window.parent.uploadOK("+status+")</script>";  
  22.    }  
service里的上传方法:

[java]  view plain  copy
  1. public int uploadCreditorInfo(Integer id,byte[] fileBatys, String exName) {  
  2.         int retCode = 10000;  
  3.         String[] arry=null;  
  4.          
  5.             //上传  
  6.             if ( exName != null && exName.length()>0) {  
  7.   
  8.                 arry = FastDFSClient.uploadFile(fileBatys,exName);  
  9.                 //更新数据库信息  
  10.                 String contracturl = "http://192.168.174.129/" + arry[0] + "/" + arry[1];  
  11.                 CreditorInfo creditorInfo = new CreditorInfo();  
  12.                 creditorInfo.setContracturl(contracturl);  
  13.                 creditorInfo.setGroups(arry[0]);  
  14.                 creditorInfo.setPath(arry[1]);  
  15.                 creditorInfo.setId(id);  
  16.   
  17.                 retCode = creditorInfoMapper.updateByPrimaryKeySelective(creditorInfo);  
  18.             }  
  19.   
  20.         return retCode;  
  21.     }  
其中service中调用的fastdfs的uploadFile()方法如下:(这也是使用fastdfs的核心部分)

[java]  view plain  copy
  1. /** 
  2.     * 文件上传 
  3.     * 
  4.     * @param fileBytes 
  5.     * @param fileExtName 
  6.     * @return 
  7.     */  
  8.    public static String[] uploadFile (byte[] fileBytes, String fileExtName) {  
  9.        TrackerServer trackerServer = null;  
  10.        StorageServer storageServer = null;  
  11.        String[] array = null;  
  12.        try {  
  13.   
  14.            //1.初始化fastdfs的连接信息配置  
  15.            ClientGlobal.init("fastdfs.properties");  
  16.   
  17.            //2.创建一个Tracker的客户端对象  
  18.            TrackerClient trackerClient = new TrackerClient();  
  19.   
  20.            //3.获得一个Tracker server对象  
  21.            trackerServer = trackerClient.getConnection();  
  22.   
  23.            //4.获得一个Storage server对象  
  24.            storageServer = trackerClient.getStoreStorage(trackerServer);  
  25.   
  26.            //5.创建一个Storage客户端对象  
  27.            StorageClient storageClient = new StorageClient(trackerServer, storageServer);  
  28.   
  29.            //6.通过storageClient客户端对象,就可以去操作fastdfs文件系统了  
  30.   
  31.            //文件上传  
  32.            array = storageClient.upload_file(fileBytes, fileExtName, null);  
  33.   
  34.        } catch (IOException e) {  
  35.            e.printStackTrace();  
  36.        } catch (MyException e) {  
  37.            e.printStackTrace();  
  38.        } finally {  
  39.            try {  
  40.                if (null != trackerServer) {  
  41.                    trackerServer.close();  
  42.                }  
  43.                if (null != storageServer) {  
  44.                    storageServer.close();  
  45.                }  
  46.            } catch (IOException e) {  
  47.                e.printStackTrace();  
  48.            }  
  49.        }  
  50.        return array;  
  51.    }  
剩下的就是页面展示了,可以根据自己的习惯,使用ajax或者其他的前端技术,这里尝试了新的技术,没有使用ajax.具体代码如下:

[html]  view plain  copy
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>  
  2. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  3. <!DOCTYPE html>  
  4. <html lang="en">  
  5. <head>  
  6.     <meta charset="utf-8">  
  7.     <title>FastDFS---Demo</title>  
  8.     <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css">  
  9.     <script src="${pageContext.request.contextPath}/js/jquery.min.js"></script>  
  10.     <script src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>  
  11. </head>  
  12.   
  13.   
  14. <body style="margin: 50px;">  
  15.   
  16. <form action="${pageContext.request.contextPath}/fastdfs/upload" class="form-horizontal" role="form" method="post" enctype="multipart/form-data" target="uploadFrame">  
  17.     <div class="form-group">  
  18.         <label class="col-sm-2 control-label">借款人真实姓名</label>  
  19.         <div class="col-sm-10">  
  20.             <input type="text" class="form-control" id="realname" name="realname" value="${creditorInfo.realname}">  
  21.         </div>  
  22.     </div>  
  23.   
  24.     <div class="form-group">  
  25.         <label class="col-sm-2 control-label">借款人手机号</label>  
  26.         <div class="col-sm-10">  
  27.             <input type="text" class="form-control" id="phone" name="phone" value="${creditorInfo.phone}">  
  28.         </div>  
  29.     </div>  
  30.   
  31.     <div class="form-group">  
  32.         <label class="col-sm-2 control-label">借款人身份证</label>  
  33.         <div class="col-sm-10">  
  34.             <input type="text" class="form-control" id="idCard" name="idCard" value="${creditorInfo.idCard}">  
  35.         </div>  
  36.     </div>  
  37.   
  38.     <div class="form-group">  
  39.         <label class="col-sm-2 control-label">借款人性别</label>  
  40.         <div class="col-sm-10">  
  41.             <input type="text" class="form-control" id="sex" name="sex" value="${creditorInfo.sex==1?"男":"女"}">  
  42.         </div>  
  43.     </div>  
  44.   
  45.     <div class="form-group">  
  46.         <label class="col-sm-2 control-label">借款人地址</label>  
  47.         <div class="col-sm-10">  
  48.             <input type="text" class="form-control" id="address" name="address" value="${creditorInfo.address}">  
  49.         </div>  
  50.     </div>  
  51.   
  52.     <div class="form-group">  
  53.         <label class="col-sm-2 control-label">上传合同</label>  
  54.         <div class="col-sm-10">  
  55.             <input type="file" class="form-control" id="fileName" name="fileName">  
  56.         </div>  
  57.     </div>  
  58.   
  59.     <div class="form-group">  
  60.         <div class="col-sm-offset-2 col-sm-10">  
  61.             <input type="hidden" class="form-control" id="id" name="id" value="${creditorInfo.id}">  
  62.             <button type="submit" class="btn btn-default">提 交</button>  
  63.         </div>  
  64.     </div>  
  65.     <%--页面不刷新上传文件,借鉴一个iframe--%>  
  66.     <iframe id="uploadFrame" name="uploadFrame" style="display: none;"></iframe>  
  67. </form>  
  68.   
  69. <%--上传后结果返回页面展示--%>  
  70. <script type="text/javascript">  
  71.     function uploadOK(status) {  
  72.         alert(status)  
  73.         if (status == 1) {  
  74.             //文件上传成功 layer弹层  
  75.             layer.confirm('文件上传成功,是否需要跳转到列表页?', function(index){  
  76.                 //do something  
  77.                 window.location.href="${pageContext.request.contextPath}/fastdfs/index"  
  78.                 layer.close(index);  
  79.             });  
  80.         } else {  
  81.             //文件上传失败  
  82.             layer.alert("Sorry,文件上传失败了")  
  83.         }  
  84.     }  
  85. </script>  
  86.   
  87. <script src="${pageContext.request.contextPath}/layer/layer.js"></script>  
  88. </body>  
  89.   
  90. </body>  
  91. </html>  
需要注意的是
[html]  view plain  copy
  1. <iframe id="uploadFrame" name="uploadFrame" style="display: none;"></iframe>  

这段代码需要同表格一起提交,它与表格头部信息其实是一一对应的,就是表单头信息里的target="uploadFrame".如果编写时没有将下面这段代码加到表单里一起提交,那么页面是跳转不了的,.

[html]  view plain  copy
  1. <form action="${pageContext.request.contextPath}/fastdfs/upload" class="form-horizontal" role="form" method="post" enctype="multipart/form-data" target="uploadFrame">  
还有一点需要注意的是,因为表单要提交本地文件,所以,务必记着在表单头信息里加入enctype="multipart/form-data",提交本地文件时要加的表单属性.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值