之前部署的java项目,打包war包后放入tomcat下,并配置conf下的server.xml,设置war包路径,设置是否自动解包与否等操作.
然后重启tomcat,稍等片刻,(web)java项目就能访问了.
而这次稍作调整,结合nginx的特性,同样war包,使用内置tomcat运行 ( 即 java -jar xxx.war )
使用原服务器tomcat做为程序内文件上传存储地.
上方为默认的nginx的配置文件
如果需要更改为 java项目的启动, java项目端口设定为90端口, proxy_pass里也更改为 :90 端口.
在使用linux文件存储路径时,在上传的控制层需要对应起来.
filepath.properties:
#头部图片img.savepath=/www/server/apache-tomcat-8.5.32/webapps/ROOT/upload/images/topimg/#新闻图片newsimg.savepath=/www/server/apache-tomcat-8.5.32/webapps/ROOT/upload/images/newsimg/#视频video.savepath=/www/server/apache-tomcat-8.5.32/webapps/ROOT/upload/video/
UploadController:
package com.xxxxx.xxx.controller;import com.alibaba.fastjson.JSONObject;import org.apache.ibatis.annotations.Param;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*;import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import java.io.File;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;/** * 文件上传 controller */@PropertySource(value = "classpath:filepath.properties")@Controller@RequestMapping("/upload")public class UploadController { @Value("${img.savepath}") private String imgPath; @Value("${video.savepath}") private String videoPath; @Value("${newsimg.savepath}") private String newsPath; /** * @param fileType 文件类型 * @param fileNum 文件编号 * @param uploadFile * @return */ @PostMapping("/{fileType}/{fileNum}") @ResponseBody public JSONObject uploadFiles(HttpServletRequest request, @PathVariable String fileType, @PathVariable Integer fileNum, @Param("uploadFile") MultipartFile uploadFile) { //默认传入文件到tomcat中 String domainPath = request.getScheme() + "://www.yourdomainname.com:8080"; System.out.println("upload!!!------start!!!----------------------!!!."); JSONObject jsonObject = new JSONObject(); /*检查文件是否上传*/ if (uploadFile.isEmpty()) { System.out.println("uploadFile is Empty!!!"); jsonObject.put("success", false); jsonObject.put("msg", "上传失败,请选择文件"); } String filePath = ""; String uploadPath = ""; /*用来区分文件类型 分别放在不同的位置*/ if (fileType.equals("img")) { //头部文件 //存储到图片区 filePath = imgPath + fileNum + "/"; uploadPath = "/upload/images/topimg/" + fileNum + "/"; } else if (fileType.equals("video")) { //存储到视频区 filePath = videoPath; uploadPath = "/upload/video/"; } else if (fileType.equals("spic")) { //存到缩略图位置 filePath = newsPath; uploadPath = "/upload/images/newsimg/"; } else if (fileType.equals("newspic")) { filePath = newsPath; uploadPath = "/upload/images/newsimg/"; } SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss"); String dateStr = sf.format(new Date()); String fileName = dateStr + "-" + uploadFile.getOriginalFilename(); System.out.println("fileName:" + fileName); File dest = new File(filePath + fileName);// System.out.println("abPath:"+filePath+fileName); try { uploadFile.transferTo(dest); jsonObject.put("success", true); jsonObject.put("errno", 0); Listpath = new ArrayList<>(); path.add(domainPath + uploadPath + fileName); System.out.println("abPath:" + domainPath + uploadPath + fileName); jsonObject.put("data", path); jsonObject.put("modelId", fileNum);// System.out.println("http://test91.ykmimi.com"+uploadPath+fileName); } catch (IOException e) { e.printStackTrace(); jsonObject.put("success", false); jsonObject.put("errno", 1); } return jsonObject; }}
此时,上传的文件会保存到properties配置的地址,访问的时候直接访问 http://yourdomainname.com:8080/upload/.../....文件后缀 即可访问到了.