Nginx+Tomcat+FastDFS文件下载模块讲解

      本文主要以示例的方式讲解,如何简单快速的搭建一个nginx+tomcat结合FastDFS文件系统,进行文件下载的文章,Nginx可以直接配置FastDFS下载,由于在实战中用处不是很大,所以暂时不讲。

      1、首先下载nginx安装包,解压并运行。

      关于nginx的安装详看http://flychao88.iteye.com/blog/1963365一文。

      关于nginx.conf文件的内容如下所示:

     

#user  nobody;
worker_processes  2;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #tcp_nopush     on;

    access_log  logs/access.log;  
  
    keepalive_timeout  65;
#   gzip  on;

    server {
        listen       80;
        server_name aa.bb.com;
        location / {  
                    proxy_connect_timeout   3;  
                    proxy_send_timeout      30;  
                    proxy_read_timeout      30;  
                    proxy_pass http://localhost;
       }  
    }

    upstream localhost {     
         server localhost:8080 weight=10;  
         server localhost:8081 weight=10;  
    }  

   注:proxy_pass后面的url域名必须要和upstream后面的标识保持一致。

 

         2、下载并配置二个tomcat,配置tomcat端口号为:8080和8081

         3、下载并安装FastDFS。

               具体请参见http://flychao88.iteye.com/blog/1725285

         4、配置spring文件如下:

             

<bean class="com.jd.fastdfs.Configuration" id="configuration"/>
    <bean id="trackerGroup" class="com.jd.fastdfs.TrackerGroup">
        <property name="configuration" ref="configuration"/>
        <property name="trackerServers">
            <list value-type="java.lang.String">
                <value>10.12.146.90:22122</value>
            </list>
        </property>
    </bean>
    <bean id="fastdfsHelper" class="com.jd.fastdfs.FastdfsHelper">
        <property name="trackerGroup" ref="trackerGroup" />
    </bean>

 

            5、配置struts.xml 文件。
<action name="oper_*"  class="fastDFSclientAction" method="{1}">
      <result name="success" type="velocity">/pages/upload.vm</result>
      <result name="downLoad" type="stream">
          <param name="contentType">application/x-zip-compressed</param>
          <param name="contentDisposition">attachment;filename="${fileName}"</param>
          <param name="inputName">downLoadFile</param>
      </result>
</action>
            6、开发Action类,代码如下:
package com.jd.paysettle.action;

import com.jd.common.struts.action.BaseAction;
import com.jd.common.web.result.Result;
import com.jd.fastdfs.FastdfsHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * User: root
 * Date: 13-10-28
 * Time: 上午3:59
 */
public class FastDFSClientAction extends BaseAction {
    private static final long serialVersionUID = -5163525262758695901L;
    private static final Logger log = LoggerFactory.getLogger(RefundAuditAction.class);

    @Autowired
    private FastdfsHelper fastdfsHelper;
    private String fileUrl = "";
    private InputStream downLoadFile;
    private String fileName;

    public String update() throws Exception {
        String[] uploadFile = fastdfsHelper.upload("group1","/root/FastDFS_v4.05.tar.gz");
        fileUrl = uploadFile[1];
        Result result = new Result();
        result.addDefaultModel("fileUrl", fileUrl);
        toVm(result);
        return SUCCESS;
    }

    public String download() throws Exception {
        byte[] bytes = fastdfsHelper.download("group1",fileUrl);
        downLoadFile = new ByteArrayInputStream(bytes);
        fileName = "fastdfs"+System.currentTimeMillis()+".tar.tz";

        return "downLoad";
    }

    public InputStream getDownLoadFile() {
        return downLoadFile;
    }

    public void setDownLoadFile(InputStream downLoadFile) {
        this.downLoadFile = downLoadFile;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFileUrl() {
        return fileUrl;
    }

    public void setFileUrl(String fileUrl) {
        this.fileUrl = fileUrl;
    }
}

            7、HTML页面内容如下:
<body>
    <br><br>upload file is : 
<a href="/paysettle/oper_download.action?fileUrl=${fileUrl}">${fileUrl}</a>
</body>
 
     8、结果如下:
Nginx+Tomcat+FastDFS文件下载模块讲解
 

 

   

 

 

 

 

 

 

关于JAVA版的FastDFS客户端,本人已经封装完毕,如果有哪位朋友需要,可以单独找我。

你可能感兴趣的:(fastDFS)