Java搭建文件服务器

技术选型:Java、maven、nginx、tomcat、window

从上传文件到文件服务器搭建一系列都会写到,前提是你会用以上的东西。这里做到资源完全分离能不跟tomcat不跟一起访问,可以扩展到分布式模式。

首先用使用maven建一个web工程,这里主要是做文件上传,所以其他的技术都不会体现,如果你需要可以加我的群联系我。

使用maven是为了方便部署,这里增加一个知识点热部署,直接上pom.xml代码

[html]  view plain  copy
  1.    
  2.  <dependencies>  
  3.          <dependency>  
  4.         <groupId>javax.servletgroupId>  
  5.         <artifactId>javax.servlet-apiartifactId>  
  6.         <version>${servlet-version}version>  
  7.         <scope>providedscope>  
  8.     dependency>  
  9.        
  10.        <dependency>  
  11.         <groupId>jstlgroupId>  
  12.         <artifactId>jstlartifactId>  
  13.         <version>${jstl-version}version>  
  14.     dependency>  
  15.     <dependency>  
  16.         <groupId>taglibsgroupId>  
  17.         <artifactId>standardartifactId>  
  18.         <version>${taglibs-version}version>  
  19.     dependency>  
  20.       
  21.     <dependency>  
  22.         <groupId>commons-fileuploadgroupId>  
  23.         <artifactId>commons-fileuploadartifactId>  
  24.         <version>${commons-fileupload-version}version>  
  25.     dependency>  
  26.       
  27.  dependencies>  
  28.     <build>  
  29.    <finalName>zwfilesysfinalName>  
  30.     <plugins>  
  31.         <plugin>  
  32.             <groupId>org.apache.maven.pluginsgroupId>  
  33.             <artifactId>maven-compiler-pluginartifactId>  
  34.             <configuration>  
  35.                 <source>${java-version}source>  
  36.                 <target>${java-version}target>  
  37.                   
  38.                 <skip>trueskip>  
  39.                 <encoding>UTF-8encoding>  
  40.             configuration>  
  41.         plugin>  
  42.         <plugin>  
  43.             <groupId>org.apache.maven.pluginsgroupId>  
  44.             <artifactId>maven-resources-pluginartifactId>  
  45.             <configuration>  
  46.                 <encoding>UTF-8encoding>  
  47.             configuration>  
  48.         plugin>  
  49.         <plugin>  
  50.             <groupId>org.apache.tomcat.mavengroupId>  
  51.             <artifactId>tomcat7-maven-pluginartifactId>  
  52.             <version>2.2version>  
  53.               
  54.              <configuration>    
  55.                 <url>http://localhost:8080/manager/texturl>    
  56.                 <username>adminusername>    
  57.                 <password>adminpassword>    
  58.                 <server>tomcatserver>    
  59.                 <path>/path>    
  60.             configuration>  
  61.         plugin>  
  62.           
  63.            
  64.     plugins>  
  65.     <resources>  
  66.         <resource>  
  67.             <directory>src/main/javadirectory>  
  68.             <includes>  
  69.                 <include>**/*.xmlinclude>  
  70.                 <include>**/*.jsinclude>  
  71.             includes>  
  72.         resource>  
  73.         <resource>  
  74.             <directory>src/main/resourcesdirectory>  
  75.         resource>  
  76.     resources>  
  77. build>  

是用热部署需要先开一个tomcat ,需要配置下tomcat的权限 tomcat-users.xml






然后使用maven命令:

 tomcat7:redeploy 
就可以进行热部署了(要先启动tomcat)
为了达到文件资源与项目分离,所以上传文件的时候是指定的绝对路径,
创建servlet类(简单的上传)
[java]  view plain  copy
  1. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  2.     PrintWriter writer = response.getWriter();  
  3.     // 得到上传文件的保存目录,将上传的文件存放于WEB-INF目录下,不允许外界直接访问,保证上传文件的安全    
  4.        String path = DateUtils.format(new Date(), "yyyyMMdd") + "/";  
  5.        File file = new File(Fina.localPath+path);    
  6.        // 判断上传文件的保存目录是否存在    
  7.        if (!file.exists() && !file.isDirectory()) {    
  8.            //目录不存在,需要创建");    
  9.            file.mkdir();// 创建目录    
  10.        }    
  11.        try {    
  12.            // 使用Apache文件上传组件处理文件上传步骤:    
  13.            // 1、创建一个DiskFileItemFactory工厂    
  14.            DiskFileItemFactory factory = new DiskFileItemFactory();    
  15.            // 2、创建一个文件上传解析器    
  16.            ServletFileUpload upload = new ServletFileUpload(factory);    
  17.            // 解决上传文件名的中文乱码    
  18.            upload.setHeaderEncoding("UTF-8");    
  19.            // 3、判断提交上来的数据是否是上传表单的数据    
  20.            if (!ServletFileUpload.isMultipartContent(request)) {    
  21.                return;    
  22.            }    
  23.            // 4、使用ServletFileUpload解析器解析上传数据,解析结果返回的是一个List集合,每一个FileItem对应一个Form表单的输入项    
  24.            List list = upload.parseRequest(request);       
  25.            String value = null;    
  26.            String filename = null ;    
  27.            for (FileItem item : list) {                    
  28.                // 如果fileitem中封装的是普通输入项的数据    
  29.                if (item.isFormField()) {    
  30.                    // 解决普通输入项的数据的中文乱码问题    
  31.                    value = item.getString("UTF-8");    
  32.                    
  33.                } else {// 如果fileitem中封装的是上传文件    
  34.                        // 得到上传的文件名称,    
  35.                    filename = item.getName();    
  36.                    if (filename == null || filename.trim().equals("")) {    
  37.                        continue;    
  38.                    }    
  39.                    filename = filename.substring(filename.lastIndexOf("\\") + 1);    
  40.                    // 获取item中的上传文件的输入流    
  41.                    InputStream in = item.getInputStream();    
  42.                    // 创建一个文件输出流    
  43.                    FileOutputStream out = new FileOutputStream(Fina.localPath + "\\"+path + filename);    
  44.                    // 创建一个缓冲区    
  45.                    byte buffer[] = new byte[1024];    
  46.                    // 判断输入流中的数据是否已经读完的标识    
  47.                    int len = 0;    
  48.                    // 循环将输入流读入到缓冲区当中,(len=in.read(buffer))>0就表示in里面还有数据    
  49.                    while ((len = in.read(buffer)) > 0) {    
  50.                        // 使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\"+ filename)当中    
  51.                        out.write(buffer, 0, len);    
  52.                    }    
  53.                    // 关闭输入流    
  54.                    in.close();    
  55.                    // 关闭输出流    
  56.                    out.close();    
  57.                    // 删除处理文件上传时生成的临时文件    
  58.                    item.delete();    
  59.                }    
  60.            }  
  61.            writer.print(""+Fina.path+path+filename);  
  62.        } catch (Exception e) {    
  63.            e.printStackTrace();    
  64.        }    
  65.       
  66. }  
public static String path ="http://192.168.1.156/";
public static String localPath ="F:/upload/";
使用到的jar包在pom.xml中有说明,页面就一个简单from表单提交文件
接下来就是访问资源了,这里使用的nginx1.6.3版本下载window版本修改nginx.conf 配置
Java搭建文件服务器_第1张图片
我这里使用的80端口,需要注意端口问题 这样配置完了以后就可以达到项目与资源分离了,就算项目挂了,还是可以访问到资源的,

不会nginx怎么办
如果你想只是资源分离,操作不分离的 可以在tomcat中虚拟一个路径,可以直接通过虚拟路径也是可以访问到的,这里资源和代码是分离的,但是操作都需要经过tomcat 没有使用到nginx来代理。

你可能感兴趣的:(文件服务器,其他服务器)