博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[linux,springboot] - 记一次java项目部署
阅读量:7071 次
发布时间:2019-06-28

本文共 3967 字,大约阅读时间需要 13 分钟。

之前部署的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);      List
path = 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/.../....文件后缀 即可访问到了.

 

转载于:https://www.cnblogs.com/ukzq/p/10687418.html

你可能感兴趣的文章
XSS零碎指南
查看>>
分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间)...
查看>>
ZStack中的编程技巧
查看>>
tcpdump -i eth0 -n -vvv src or dst port 443
查看>>
如何给你的Android 安装文件(APK)瘦身
查看>>
如何把SKYPE的发送消息由enter改为ctrl+enter?
查看>>
GitHub 基本常用知识解答
查看>>
一个有趣的算法题。。。
查看>>
spring - ioc和aop
查看>>
svn branching and merging
查看>>
Linux系统基础网络配置
查看>>
拥抱PBO(基于项目的组织)聚焦核心价值创造
查看>>
这是要逆天么,看我控制台程序玩Microsoft XPS Document 打印
查看>>
jmeter源码编译
查看>>
jquery 取id模糊查询
查看>>
谈谈转行
查看>>
收集的网络上大型的开源图像处理软件代码(提供下载链接)
查看>>
IOS调试下载的demo出现说项目不能在什么的SDK调试
查看>>
#周末课堂# 【Linux + JVM + Mysql高级性能优化班】(火热报名中~~~)
查看>>
HTTP常用Header讲解
查看>>