一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - SpringMVC跨服務(wù)器上傳文件中出現(xiàn)405錯誤的解決

SpringMVC跨服務(wù)器上傳文件中出現(xiàn)405錯誤的解決

2022-01-25 00:57Haochengqi Java教程

這篇文章主要介紹了SpringMVC跨服務(wù)器上傳文件中出現(xiàn)405錯誤的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

SpringMVC跨服務(wù)器上傳文件中出現(xiàn)405錯誤

下面是 應(yīng)用服務(wù)器 的代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.itheima.controller;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;
 
@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/fileupload3")
    public String fileupload3(MultipartFile upload) throws Exception{
        System.out.println("跨服務(wù)器文件上傳....");
 
        //定義上傳文件服務(wù)器的路徑
        String path = "http://localhost:9090/uploads/";
        System.out.println(upload.getBytes());
 
        //定義上傳文件項
        //獲取上傳文件的名稱
        String filename = upload.getOriginalFilename();
        //把文件的名稱設(shè)置成唯一值,uuid
        String uuid = UUID.randomUUID().toString().replace("-","");
        filename = uuid + "_" + filename;
 
        //創(chuàng)建客戶端對象
        Client client = Client.create();
 
        //和圖片服務(wù)器進行連接
        WebResource webResource = client.resource(path + filename);  //相當(dāng)于創(chuàng)建一個連接對象
 
        //上傳文件按
        webResource.put(upload.getBytes());
        return "success";
    }
 
    /**
     * SpringMVC文件上傳
     * @return
     */
    @RequestMapping("/fileupload2")
    public String fileuoload2(HttpServletRequest request, MultipartFile upload) throws Exception {
        System.out.println("springmvc文件上傳...");
 
        // 使用fileupload組件完成文件上傳
        // 上傳的位置
        String path = request.getSession().getServletContext().getRealPath("/uploads/");
        // 判斷,該路徑是否存在
        File file = new File(path);
        if(!file.exists()){
            // 創(chuàng)建該文件夾
            file.mkdirs();
        }
 
        // 說明上傳文件項
        // 獲取上傳文件的名稱
        String filename = upload.getOriginalFilename();
        // 把文件的名稱設(shè)置唯一值,uuid
        String uuid = UUID.randomUUID().toString().replace("-", "");
        filename = uuid+"_"+filename;
        // 完成文件上傳
        upload.transferTo(new File(path,filename));
        return "success";
    }
 
    /**
     * 文件上傳
     * @return
     */
    @RequestMapping("/fileupload1")
    public String fileuoload1(HttpServletRequest request) throws Exception {
        System.out.println("文件上傳...");
 
        // 使用fileupload組件完成文件上傳
        // 上傳的位置
        String path = request.getSession().getServletContext().getRealPath("/uploads/");
        // 判斷,該路徑是否存在
        File file = new File(path);
        if(!file.exists()){
            // 創(chuàng)建該文件夾
            file.mkdirs();
        }
 
        // 解析request對象,獲取上傳文件項
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        // 解析request
        List<FileItem> items = upload.parseRequest(request);
        // 遍歷
        for(FileItem item:items){
            // 進行判斷,當(dāng)前item對象是否是上傳文件項
            if(item.isFormField()){
                // 說明普通表單向
            }else{
                // 說明上傳文件項
                // 獲取上傳文件的名稱
                String filename = item.getName();
                // 把文件的名稱設(shè)置唯一值,uuid
                String uuid = UUID.randomUUID().toString().replace("-", "");
                filename = uuid+"_"+filename;
                // 完成文件上傳
                item.write(new File(path,filename));
                // 刪除臨時文件
                item.delete();
            }
        }
        return "success";
    }
}

springmvc.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
 
    <!-- 開啟注解掃描 -->
    <context:component-scan base-package="com.itheima"/>
 
    <!-- 視圖解析器對象 -->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
 
    <!--前端控制器,哪些靜態(tài)資源不攔截-->
    <mvc:resources location="/css/" mapping="/css/**"/>
    <mvc:resources location="/images/" mapping="/images/**"/>
    <mvc:resources location="/js/" mapping="/js/**"/>
 
    <!--前端控制器,哪些靜態(tài)資源不攔截-->
    <mvc:resources location="/css/" mapping="/css/**"/>
    <mvc:resources location="/images/" mapping="/images/**"/>
    <mvc:resources location="/js/" mapping="/js/**"/>
 
    <!--配置文件解析器對象-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10485760" />
    </bean>
 
    <!-- 開啟SpringMVC框架注解的支持 -->
    <mvc:annotation-driven />
 
</beans>

success.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/5/4
Time: 21:58
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>上傳文件成功</h3>
</body>
</html>

web.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at
      http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
 
<!--
  - This is the Cocoon web-app configurations file
  -
  - $Id$
  -->
<!--suppress ALL -->
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
  <display-name>Archetype Created Web Application</display-name>
 
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
 
  <servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
    <init-param>
      <param-name>debug</param-name>
      <param-value>0</param-value>
    </init-param>
    <init-param>
      <param-name>listings</param-name>
      <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
 
  <!--配置解決中文亂碼的過濾器-->
  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

index.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<%--
Created by IntelliJ IDEA.
User: QHC
Date: 2019/10/9
Time: 13:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>文件上傳</title>
</head>
<body>
<%--不知道為啥,在臺式機可以跑成功,在筆記本就報錯,難道是tomcat的版本的原因?--%>
<h3>傳統(tǒng)文件上傳</h3>
<form action="/user/fileupload1" method="post" enctype="multipart/form-data">
選擇文件:<input type="file" name="upload"/><br>
<input type="submit" value="上傳"/>
</form>
<h3>SpringMVC文件上傳</h3>
<form action="/user/fileupload2" method="post" enctype="multipart/form-data">
選擇文件:<input type="file" name="upload"/><br>
<input type="submit" value="上傳"/>
</form>
<h3>跨服務(wù)器上傳文件</h3>
<form action="/user/fileupload3" method="post" enctype="multipart/form-data">
選擇文件:<input type="file" name="upload" /><br/>
<input type="submit" value="上傳" />
</form>
<a href="/user/testGetRealPath" rel="external nofollow" >查看request.getSession().getServletContext().getRealPath("\uploads\")的值</a>
</body>
</html>

SpringMVC跨服務(wù)器上傳文件中出現(xiàn)405錯誤的解決

如果遇到報錯405,PUT http://localhost:9090/uploads/.........

只需要在文件服務(wù)器中的 web.xml 中加入下面的代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<servlet>
        <servlet-name>default</servlet-name>
        <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>0</param-value>
        </init-param>
        <init-param>
            <param-name>readonly</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>listings</param-name>
            <param-value>false</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

重點來了~

idea中springmvc跨服務(wù)器上傳文件報405錯誤,修改了web.xml一樣報錯

這個問題是因為你使用的文件服務(wù)器的Tomcat使用的是exploded模式部署,修改的Tomcat本地conf下的web.xml對exploded的項目沒有生效,此時應(yīng)該使用war包模式進行部署,本地修改的web.xml文件繼續(xù)保持修改狀態(tài),并且修改Application context不為/,可以修改為:/+任意文件名

然后再重新部署一下Tomcat服務(wù)器,此時不再報錯。(注意要修改一下代碼中的文件上傳路徑)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/xiaoqi44325234/article/details/102501555

延伸 · 閱讀

精彩推薦
  • Java教程升級IDEA后Lombok不能使用的解決方法

    升級IDEA后Lombok不能使用的解決方法

    最近看到提示IDEA提示升級,尋思已經(jīng)有好久沒有升過級了。升級完畢重啟之后,突然發(fā)現(xiàn)好多錯誤,本文就來介紹一下如何解決,感興趣的可以了解一下...

    程序猿DD9332021-10-08
  • Java教程小米推送Java代碼

    小米推送Java代碼

    今天小編就為大家分享一篇關(guān)于小米推送Java代碼,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧...

    富貴穩(wěn)中求8032021-07-12
  • Java教程Java8中Stream使用的一個注意事項

    Java8中Stream使用的一個注意事項

    最近在工作中發(fā)現(xiàn)了對于集合操作轉(zhuǎn)換的神器,java8新特性 stream,但在使用中遇到了一個非常重要的注意點,所以這篇文章主要給大家介紹了關(guān)于Java8中S...

    阿杜7482021-02-04
  • Java教程Java使用SAX解析xml的示例

    Java使用SAX解析xml的示例

    這篇文章主要介紹了Java使用SAX解析xml的示例,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下...

    大行者10067412021-08-30
  • Java教程xml與Java對象的轉(zhuǎn)換詳解

    xml與Java對象的轉(zhuǎn)換詳解

    這篇文章主要介紹了xml與Java對象的轉(zhuǎn)換詳解的相關(guān)資料,需要的朋友可以參考下...

    Java教程網(wǎng)2942020-09-17
  • Java教程Java BufferWriter寫文件寫不進去或缺失數(shù)據(jù)的解決

    Java BufferWriter寫文件寫不進去或缺失數(shù)據(jù)的解決

    這篇文章主要介紹了Java BufferWriter寫文件寫不進去或缺失數(shù)據(jù)的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望...

    spcoder14552021-10-18
  • Java教程20個非常實用的Java程序代碼片段

    20個非常實用的Java程序代碼片段

    這篇文章主要為大家分享了20個非常實用的Java程序片段,對java開發(fā)項目有所幫助,感興趣的小伙伴們可以參考一下 ...

    lijiao5352020-04-06
  • Java教程Java實現(xiàn)搶紅包功能

    Java實現(xiàn)搶紅包功能

    這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)搶紅包功能,采用多線程模擬多人同時搶紅包,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙...

    littleschemer13532021-05-16
主站蜘蛛池模板: 国产麻豆91欧美一区二区 | 国产真实伦对白在线播放 | 四虎最新网址在线观看 | 欧亚精品一区二区三区 | 欧美一区二区三区精品 | 国产亚洲精品一区二区在线播放 | 亚洲精品国产在线观看 | 亚洲乱亚洲乱妇41p国产成人 | 国产精品俺来也在线观看了 | 免费一级特黄特色大片在线 | 日韩欧美高清视频 | 91精品国产91久久久久 | 国产精品久久99 | 双性np肉文 | eeuss18影院www国产 | 成人网子 | 女人麻豆国产香蕉久久精品 | 99久久国产综合精品女小说 | 亚洲国产第一 | 亚洲精品中文字幕久久久久久 | 女人和男人搞基 | 热99这里有精品综合久久 | 热99精品在线 | 亚洲精品国产精品精 | 日韩欧美精品一区二区 | 9420高清视频在线观看网百度 | 日本大尺度动漫在线观看缘之空 | 我和么公的秘密小说免费 | 女子监狱第二季未删减在线看 | 亚洲天堂网在线观看视频 | 美女gif趴跪式抽搐动态图 | 亚欧成人一区二区 | 白发在线视频播放观看免费 | 午夜私人影院在线观看 | 亚洲人成高清毛片 | 1024亚洲精品国产 | 黄动漫车车好快的车车双女主 | 午夜无码片在线观看影院 | 成人国产第一区在线观看 | 青青在线香蕉国产精品 | 秋霞午夜视频 |