技术选型:Java、maven、nginx、tomcat、window
从上传文件到文件服务器搭建一系列都会写到,前提是你会用以上的东西。这里做到资源完全分离能不跟tomcat不跟一起访问,可以扩展到分布式模式。
首先用使用maven建一个web工程,这里主要是做文件上传,所以其他的技术都不会体现,如果你需要可以加我的群联系我。
使用maven是为了方便部署,这里增加一个知识点热部署,直接上pom.xml代码
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-version}</version>
<scope>provided</scope>
</dependency>
<!-- JSP视图所需jar -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl-version}</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>${taglibs-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload-version}</version>
</dependency>
</dependencies>
<build>
<finalName>zwfilesys</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
<!-- true:跳过测试 -->
<skip>true</skip>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<!-- <configuration>
<path>/</path>
<charset>UTF-8</charset>
<port>80</port>
<uriEncoding>UTF-8</uriEncoding>
</configuration> -->
<!-- 热部署 -->
<configuration>
<url>http://localhost:8080/manager/text</url>
<username>admin</username>
<password>admin</password>
<server>tomcat</server>
<path>/</path>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.js</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
是用热部署需要先开一个tomcat ,需要配置下tomcat的权限 tomcat-users.xml
<role rolename="admin" />
<role rolename="manager" />
<role rolename="manager-gui" />
<role rolename="manager-script" />
<user username="admin" password="admin" roles="admin,manager,manager-gui,manager-script" />
然后使用maven命令:
tomcat7:redeploy
就可以进行热部署了(要先启动tomcat)
为了达到文件资源与项目分离,所以上传文件的时候是指定的绝对路径,
创建servlet类(简单的上传)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter writer = response.getWriter();
// 得到上传文件的保存目录,将上传的文件存放于WEB-INF目录下,不允许外界直接访问,保证上传文件的安全
String path = DateUtils.format(new Date(), "yyyyMMdd") + "/";
File file = new File(Fina.localPath+path);
// 判断上传文件的保存目录是否存在
if (!file.exists() && !file.isDirectory()) {
//目录不存在,需要创建");
file.mkdir();// 创建目录
}
try {
// 使用Apache文件上传组件处理文件上传步骤:
// 1、创建一个DiskFileItemFactory工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
// 2、创建一个文件上传解析器
ServletFileUpload upload = new ServletFileUpload(factory);
// 解决上传文件名的中文乱码
upload.setHeaderEncoding("UTF-8");
// 3、判断提交上来的数据是否是上传表单的数据
if (!ServletFileUpload.isMultipartContent(request)) {
return;
}
// 4、使用ServletFileUpload解析器解析上传数据,解析结果返回的是一个List<FileItem>集合,每一个FileItem对应一个Form表单的输入项
List<FileItem> list = upload.parseRequest(request);
String value = null;
String filename = null ;
for (FileItem item : list) {
// 如果fileitem中封装的是普通输入项的数据
if (item.isFormField()) {
// 解决普通输入项的数据的中文乱码问题
value = item.getString("UTF-8");
} else {// 如果fileitem中封装的是上传文件
// 得到上传的文件名称,
filename = item.getName();
if (filename == null || filename.trim().equals("")) {
continue;
}
filename = filename.substring(filename.lastIndexOf("\\") + 1);
// 获取item中的上传文件的输入流
InputStream in = item.getInputStream();
// 创建一个文件输出流
FileOutputStream out = new FileOutputStream(Fina.localPath + "\\"+path + filename);
// 创建一个缓冲区
byte buffer[] = new byte[1024];
// 判断输入流中的数据是否已经读完的标识
int len = 0;
// 循环将输入流读入到缓冲区当中,(len=in.read(buffer))>0就表示in里面还有数据
while ((len = in.read(buffer)) > 0) {
// 使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\"+ filename)当中
out.write(buffer, 0, len);
}
// 关闭输入流
in.close();
// 关闭输出流
out.close();
// 删除处理文件上传时生成的临时文件
item.delete();
}
}
writer.print(""+Fina.path+path+filename);
} catch (Exception e) {
e.printStackTrace();
}
}
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 配置
我这里使用的80端口,需要注意端口问题 这样配置完了以后就可以达到项目与资源分离了,就算项目挂了,还是可以访问到资源的,
不会nginx怎么办
如果你想只是资源分离,操作不分离的 可以在tomcat中虚拟一个路径,可以直接通过虚拟路径也是可以访问到的,这里资源和代码是分离的,但是操作都需要经过tomcat 没有使用到nginx来代理。
如果以上不明白可以加我的群联系我 278947305
希望以上能帮助到你。