springboot部署Tomcat,获取路径及测试无法找到资源文件

1、获取路径方式:

 

     @GetMapping("/t")
       @ResponseBody
       public String wtt(HttpServletRequest request) {
           File path = null;
           File upload =null;
           try {
                path = new File(ResourceUtils.getURL("classpath:").getPath());
                if(!path.exists()) path = new File("");
                //如果上传目录为/static/images/upload/,则可以如下获取:
                upload= new File(path.getAbsolutePath(),"static/images/upload");
                if(!upload.exists()) upload.mkdirs();
                //在开发测试模式时,得到的地址为:{项目跟目录}/target/static/images/upload/
                //在打包成jar正式发布时,得到的地址为:{发布jar包目录}/static/images/upload/    
           } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
           }
           try{
                BufferedWriter bw = new BufferedWriter(new FileWriter("填写自己想打印到计算机的位置"));
                bw.write(path.getAbsolutePath());
                bw.newLine();
                bw.write(upload.getAbsolutePath());
                bw.newLine();
                bw.write(ClassUtils.getDefaultClassLoader().getResource("").getPath());
                bw.newLine();
                bw.write(System.getProperty("user.dir"));
                bw.newLine();
                bw.write(ResourceUtils.getURL("classpath:").getPath());
                bw.newLine();
                bw.write(request.getSession().getServletContext().getRealPath(""));
                bw.newLine();
                bw.write(request.getSession().getServletContext().getRealPath("/"));
                bw.close();
           }catch(IOException e){
               e.printStackTrace();
           }
           return "tttt";
       }

2、关于Tomcat出现

The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

 

资源路径问题:检查自己的路径问题。

 

 

 

 

 

 

 

 

以下转载内容:https://blog.csdn.net/qq_34381084/article/details/81485319

springboot项目如何打包成war包

 

一、修改打包形式

在pom.xml里设置 <packaging>war</packaging>

二、移除嵌入式tomcat插件

在pom.xml里找到spring-boot-starter-web依赖节点,将<exclusions></exclusions>之间的内容删除,

 
  1. <dependency>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-web</artifactId>

  4. <!-- 移除嵌入式tomcat插件 -->

  5. <exclusions>

  6. <exclusion>

  7. <groupId>org.springframework.boot</groupId>

  8. <artifactId>spring-boot-starter-tomcat</artifactId>

  9. </exclusion>

  10. </exclusions>

  11. </dependency>

三、添加servlet-api的依赖

下面两种方式都可以,任选其一

 
  1. <dependency>

  2. <groupId>javax.servlet</groupId>

  3. <artifactId>javax.servlet-api</artifactId>

  4. <version>3.1.0</version>

  5. <scope>provided</scope>

  6. </dependency>


 
  1. <dependency>

  2. <groupId>org.apache.tomcat</groupId>

  3. <artifactId>tomcat-servlet-api</artifactId>

  4. <version>8.0.36</version>

  5. <scope>provided</scope>

  6. </dependency>

四、修改启动类,并重写初始化方法

我们平常用main方法启动的方式,都有一个App的启动类,代码如下:

 
  1. @SpringBootApplication

  2. public class Application {

  3. public static void main(String[] args) {

  4. SpringApplication.run(Application.class, args);

  5. }

  6. }

我们需要类似于web.xml的配置方式来启动spring上下文了,在Application类的同级添加一个SpringBootStartApplication类,其代码如下:

 
  1. /**

  2. * 修改启动类,继承 SpringBootServletInitializer 并重写 configure 方法

  3. */

  4. public class SpringBootStartApplication extends SpringBootServletInitializer {

  5.  
  6. @Override

  7. protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {

  8. // 注意这里要指向原先用main方法执行的Application启动类

  9. return builder.sources(Application.class);

  10. }

  11. }

五、打包部署

在项目根目录下(即包含pom.xml的目录),点击键盘的shift键加上鼠标右键,出现命令窗口,选择powershell命令,然后在命令行里输入: mvn clean package即可, 等待打包完成,出现[INFO] BUILD SUCCESS即为打包成功。
mvn clean package -Dmaven.test.skip=true(我用这个打包报错)

 


然后把target目录下的war包放到tomcat的webapps目录下,启动tomcat,切记页面中访问controller的路劲要加上项目名,才能正常访问,即可自动解压部署。 
最后在浏览器中输入

http://localhost:[端口号]/[打包项目名]/

发布成功

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值