nginx实现网站图片上载服务器
我这里就写两个例子:
Eg1:电商网站分前台和后台,后台维护商品和供应商即后台上传图片前台展示,现在要求前台部署在A服务器,前台部署在B服务器上且用nginx做图片服务器并将nginx服务装在A服务器上,图片上传到A服务器本地xx文件夹下。
这中情况就很简单,我们只需要将后台上传图片的url设置为A服务本地创建的xx文件,并修改conf/nginx.conf中
server {
listen 80;
server_name 127.0.0.1;
#charset koi8-r;
#access_log logs/host.access.log main;
location /ekaImage/{
alias D:\ekaImage;
}
前台访问时直接http://A服务IP: ekaImage/XX/xx.jpg即可
Eg2:后台服务部署在A服务器上,前台部署在B服务器,图片服务器部署在C服务器上
我们是单独将图片上传写了个APP部署在C服务器然后后台上传图片是调C服务器部署的图片服务器应用提供的上传图片的方法并将图片存放在C服务器的本地文件夹xxx下且在nginx.conf里修改为C服务器IP,前台访问时直接按http:+C服务器IP+文件路径+图片路径
图片服务器应用:
package com.nginx.service;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import https://www.360docs.net/doc/fc18410357.html,mons.fileupload.FileItem;
import https://www.360docs.net/doc/fc18410357.html,mons.fileupload.FileItemFactory;
import
https://www.360docs.net/doc/fc18410357.html,mons.fileupload.FileUploadException; import
https://www.360docs.net/doc/fc18410357.html,mons.fileupload.disk.DiskFileItemFactor y;
import
https://www.360docs.net/doc/fc18410357.html,mons.fileupload.servlet.ServletFileUplo ad;
import org.apache.log4j.Logger;
/**
* Servlet implementation class nginxServlet
*/
public class nginxServlet extends HttpServlet {
private Logger logger=
Logger.getLogger(nginxServlet.class);
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public nginxServlet() {
super();
https://www.360docs.net/doc/fc18410357.html,(" nginxServlet init .......... "); }
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
https://www.360docs.net/doc/fc18410357.html,(" nginxServlet
doGet ............................ ");
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
https://www.360docs.net/doc/fc18410357.html,(" nginxServlet
doPost ............................ ");
FileItemFactory factory = new
DiskFileItemFactory();
ServletFileUpload upload = new
ServletFileUpload(factory);
try {
List items = upload.parseRequest(request); Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
//普通文本信息处理
String paramName =
item.getFieldName();
String paramValue = item.getString();
https://www.360docs.net/doc/fc18410357.html,(paramName + ":" + paramValue);
} else {
//上传文件信息处理
String fileName = item.getName(); String filePath="";
if(fileName.indexOf("/")!=-1){
filePath =
fileName.substring(0,https://www.360docs.net/doc/fc18410357.html,stIndexOf("/"));
fileName =
fileName.substring(https://www.360docs.net/doc/fc18410357.html,stIndexOf("/"),fileName
.length());
}
https://www.360docs.net/doc/fc18410357.html,("item :
"+item.getName());
https://www.360docs.net/doc/fc18410357.html,(" fname : "+fileName);
https://www.360docs.net/doc/fc18410357.html,(" path : "+filePath);
File f = new File(filePath);
if(!f.exists()){
f.mkdirs();
}
byte[] data = item.get();
FileOutputStream fos = new
FileOutputStream(item.getName());
fos.write(data);
fos.close();
}
}
} catch (FileUploadException e) {
logger.error(" nginxServlet doPOST exception :"+e.getMessage());
e.printStackTrace();
}
}
}
Web.xml:
配置nginx/nginx.conf
server {
listen 80;
server_name 127.0.0.1;
#charset koi8-r;
#access_log logs/host.access.log main;
location /ekaImage/{
alias /wls/ekaImage/;
}
后台服务代码:
#图片服务器
imgIp= http://192.168.221.91/ekaImage前台
src=”${ imgIp }”>
#图片上传到哪个地方
uploadFilePath = /wls/ekaImage/
#nginx图片服务器应用
Nginxpath=http://192.168.221.91:8080/nginxService/ngi nxServlet
后台上传图片调图片服务器http://192.168.221.91:8080/nginxService/nginxServlet 如service:
@RequestMapping(value = "uploadFile")
@ResponseBody
public Map
uploadFile(MultipartHttpServletRequest request, HttpServletResponse response, String type) throws IOException{
Map
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy/MM/dd/HH");
try {
MultipartHttpServletRequest multipartRequest = request;
MultipartFile file =
multipartRequest.getFile("file");
String fileName = file.getOriginalFilename();
String suffix =
fileName.substring(https://www.360docs.net/doc/fc18410357.html,stIndexOf("."));
String destFileName = new Date().getTime() + suffix;
InputStream stream = file.getInputStream();
//Nginx 图片服务器地址
String serverPath =
PropertiesUtil.getValue("nginxpath");
String path =
PropertiesUtil.getValue("uploadFilePath")+ dateformat.format(new Date());
upload(serverPath,file.getInputStream(),path,destFi leName);
https://www.360docs.net/doc/fc18410357.html,("-----------path--------------"+path);
String logoRealPathDir = path;
https://www.360docs.net/doc/fc18410357.html,("-----------logoRealPathDir 2
--------------"+logoRealPathDir);
String returnPath = logoRealPathDir +"/" + destFileName;
map.put("status", "ok");
map.put("filePath", returnPath);
map.put("fileName", fileName);
} catch (Exception e) {
logger.error("文件上传失败或者json.put值失败,失败信息:" + e);
try {
map.put("status", "fail");
} catch (JSONException e2) {
logger.error("json.put值失败,失败信息:" + e2);
}
}
return map;
}