文件服务器解决方案(项目源码)

任何强大的单一服务器都满足不了大型网站持续增长的业务需求,网站发展到一定程序,应用服务器就要跟文件存储分离,创建文件服务器分摊应用服务器的压力。文件服务器是为网络上各工作站提供完整数据、文件、目录等信息共享,对网络文件实行统一管理的服务器。它能进行文件建立、删除、打开、关闭、读写等操作。当然文件服务器并不是单一存在的,如果存在单点故障,对项目的影响将是巨大的,所以前期分布式文件系统也是项目上线初期必须的,做好容灾备份。对分布式文件系统感兴趣的同学可以看下FastDFShttp://www.52itstyle.com/thread-22615-1-1.html

一、项目需求
       在B项目中前台上传一个文件,后台只是负责获取文件,然后需要调用rest接口把文件传给另一个系统A(文件服务系统),文件服务器系统会返回给我需要的状态,然后根据状态判断是否上传成功。

文件服务器解决方案(项目源码)



二、文件服务解决方案
1. 使用NSF 映射远程磁盘目录,将A的某个磁盘目录映射到B的某个文件夹,那么,在B上传文件时,只需要复制一份到这个映射目录就可以了,系统会直接传送的A服务器的目录里,如果实在局域网这个速度是可以忽略速度影响的,但是如果不是在局域网可能会有传输速度影响。


配置详见:http://www.52itstyle.com/thread-10764-1-1.html


2.利用服务器rsync的同步工具。在B架设rsync的服务端,设定需要同步的文件夹,在A设定rsync的客户端,设定同步的来源服务器和对应的文件夹。当B的上传文件夹变动时,rsync会自动同步一份到A的客户目录。


配置详见:http://www.52itstyle.com/thread-17263-1-1.html


3.如果以上两种方案你都不想使用,可以在A部署一个底层脚本,用来接收post文件,可以自己设定一个**。当B有文件上传时,B服务器用java的httpClient直接重新post一份到A的接口,然后在A的接口设定逻辑存放到相应的目录。


4.如果文件大小不是太大,B服务器还可以利用各种中转程序,例如先存到mysql,或者nosql的存储里,然后在A服务器上自动去抓取下来。


5.当然你还可以使用静态资源云存储,比如七牛,阿里云,使用它们的接口就能把资源上传到它们的服务器,接口十分简单,费用非常便宜,比自建资源服务器便宜很多,但是毕竟不是自己的。


这里主要简单实现第三种方案(使用语言JAVA):


新建客户端 用于上传
HttpPostClient:

  1. package com.itstyle.web;


  2. import java.io.File;

  3. import java.io.IOException;


  4. import org.apache.http.HttpEntity;

  5. import org.apache.http.HttpResponse;

  6. import org.apache.http.HttpStatus;

  7. import org.apache.http.ParseException;

  8. import org.apache.http.client.HttpClient;

  9. import org.apache.http.client.methods.HttpPost;

  10. import org.apache.http.entity.mime.MultipartEntity;

  11. import org.apache.http.entity.mime.content.FileBody;

  12. import org.apache.http.entity.mime.content.StringBody;

  13. import org.apache.http.impl.client.DefaultHttpClient;

  14. import org.apache.http.util.EntityUtils;

  15. /**

  16. * 客服端

  17. * 创建者       xxx

  18. * 创建时间        2016年4月14日

  19. * 科帮网 http://www.52itstyle.com

  20. *

  21. */

  22. public class HttpPostClient {

  23.         public void SubmitPost(String url, String filename, String filepath) {


  24.                 HttpClient httpclient = new DefaultHttpClient();


  25.                 try {


  26.                         HttpPost httppost = new HttpPost(url);


  27.                         FileBody bin = new FileBody(new File(filepath + File.separator + filename));


  28.                         StringBody comment = new StringBody(filename);


  29.                         MultipartEntity reqEntity = new MultipartEntity();

  30.                         reqEntity.addPart("file", bin);// file为请求后台的File upload;属性

  31.                         reqEntity.addPart("filename", comment);// filename为请求后台的普通参数;属性

  32.                         httppost.setEntity(reqEntity);


  33.                         HttpResponse response = httpclient.execute(httppost);


  34.                         int statusCode = response.getStatusLine().getStatusCode();


  35.                         if (statusCode == HttpStatus.SC_OK) {


  36.                                 System.out.println("服务器正常响应.....");


  37.                                 HttpEntity resEntity = response.getEntity();


  38.                                 System.out.println(EntityUtils.toString(resEntity));// httpclient自带的工具类读取返回数据


  39.                                 System.out.println(resEntity.getContent());


  40.                                 EntityUtils.consume(resEntity);

  41.                         }


  42.                 } catch (ParseException e) {

  43.                         e.printStackTrace();

  44.                 } catch (IOException e) {

  45.                         e.printStackTrace();

  46.                 } finally {

  47.                         try {

  48.                                 httpclient.getConnectionManager().shutdown();

  49.                         } catch (Exception ignore) {

  50.                                 

  51.                         }

  52.                 }

  53.         }

  54.         /**

  55.          * 自定义文件名称  和路径  win环境下测试

  56.          * @param  args

  57.          */

  58.         public static void main(String[] args) {

  59.                 HttpPostClient httpPostClient = new HttpPostClient();

  60.                 httpPostClient.SubmitPost("http://127.0.0.1:8080/acts_upload/receiveData","test.zip", "D://test");

  61.         }

  62. }



复制代码




新建服务端 用于接收
HttpPostServer:

  1. package com.itstyle.web;


  2. import java.io.File;

  3. import java.io.FileOutputStream;

  4. import java.io.IOException;

  5. import java.io.InputStream;

  6. import java.io.PrintWriter;

  7. import java.util.ArrayList;

  8. import java.util.Iterator;

  9. import java.util.List;


  10. import javax.servlet.ServletException;

  11. import javax.servlet.http.HttpServlet;

  12. import javax.servlet.http.HttpServletRequest;

  13. import javax.servlet.http.HttpServletResponse;


  14. import org.apache.commons.fileupload.FileItem;

  15. import org.apache.commons.fileupload.FileItemFactory;

  16. import org.apache.commons.fileupload.disk.DiskFileItemFactory;

  17. import org.apache.commons.fileupload.servlet.ServletFileUpload;

  18. /**

  19. * 服务端 接收文件

  20. * 创建者        xxx

  21. * 创建时间        2016年4月14日

  22. * 科帮网 http://www.52itstyle.com

  23. */

  24. public class HttpPostServer extends HttpServlet {

  25.         private static final long serialVersionUID = -1002826847460469784L;


  26.         @Override

  27.         public void init() throws ServletException {


  28.         }


  29.         @SuppressWarnings("unchecked")

  30.         public void doGet(HttpServletRequest request, HttpServletResponse response)

  31.                         throws ServletException, IOException {

  32.                 PrintWriter out = null;

  33.                 

  34.                 response.setContentType("text/html;charset=UTF-8");

  35.         String basePath = "d://test1";

  36.                 FileItemFactory factory = new DiskFileItemFactory();

  37.                 ServletFileUpload upload = new ServletFileUpload(factory);

  38.                 File directory = null;

  39.                 List<FileItem> items = new ArrayList<FileItem>();

  40.                 String message = "";

  41.                 try {

  42.                         items = upload.parseRequest(request);

  43.                         // 得到所有的文件

  44.                         Iterator<FileItem> it = items.iterator();

  45.                         while (it.hasNext()) {

  46.                                 FileItem fItem = (FileItem) it.next();

  47.                 if(!fItem.isFormField()){

  48.                         String name = fItem.getName();

  49.                         if (name != null && !("".equals(name))) {

  50.                                 name = name.substring(name.lastIndexOf(File.separator) + 1);

  51.                                 directory = new File(basePath);

  52.                                 directory.mkdirs();

  53.                                 String filePath = (basePath)  + File.separator + name;

  54.                                 InputStream is = fItem.getInputStream();

  55.                                 FileOutputStream fos = new FileOutputStream(filePath);

  56.                                 byte[] buffer = new byte[1024];

  57.                                 while (is.read(buffer) > 0) {

  58.                                         fos.write(buffer, 0, buffer.length);

  59.                                 }

  60.                                 fos.flush();

  61.                                 fos.close();

  62.                         }

  63.                 }

  64.                         }

  65.                         message = "{success:true, msg:'接收成功'}";

  66.                 } catch (Exception e) {

  67.                         message = "{success:false, msg:'读取http请求属性值出错!'}";

  68.                         e.printStackTrace();

  69.                 }finally{

  70.                         out = response.getWriter();

  71.                         out.print(message);

  72.                         out.close();

  73.                 }

  74.         }

  75.         public void doPost(HttpServletRequest request, HttpServletResponse response)

  76.                         throws ServletException, IOException {

  77.                 doGet(request, response);

  78.         }

  79. }



复制代码


所需要JAR包

commons-codec-1.6.jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.1.1.jar
fluent-hc-4.2.jar
httpclient-4.2.jar
httpclient-cache-4.2.jar
httpcore-4.2.jar
httpmime-4.2.jar

项目下载地址:点击下载


密码:

本帖隐藏的内容

56d8


转载于:https://my.oschina.net/52love/blog/661829