最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2021年09月30日 16:21:19   作者:Haochengqi  
這篇文章主要介紹了SpringMVC跨服務(wù)器上傳文件中出現(xiàn)405錯誤的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

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

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

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);  //相當于創(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){
            // 進行判斷,當前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

<?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

<%--
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

<?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

<%--
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>

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

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

<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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 使用jvm sandbox對三層嵌套類型的改造示例

    使用jvm sandbox對三層嵌套類型的改造示例

    這篇文章主要為大家介紹了使用jvm sandbox對三層嵌套類型的改造示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • Java基本數(shù)據(jù)類型存儲在JVM中的存儲位置介紹

    Java基本數(shù)據(jù)類型存儲在JVM中的存儲位置介紹

    這篇文章主要介紹了Java基本數(shù)據(jù)類型存儲在JVM中的存儲位置,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Java中的

    Java中的"goto"語句妙用

    這篇文章主要介紹了Java中的"goto"語句妙用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 深入理解ThreadLocal工作原理及使用示例

    深入理解ThreadLocal工作原理及使用示例

    這篇文章主要介紹了深入理解ThreadLocal工作原理及使用示例,涉及ThreadLocal<T> 簡介和使用示例及ThreadLocal<T>的原理等相關(guān)內(nèi)容,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • Java高級語法學(xué)習(xí)之反射詳解

    Java高級語法學(xué)習(xí)之反射詳解

    java的泛型和反射機制一直很難理解和應(yīng)用,下面這篇文章主要給大家介紹了關(guān)于Java高級語法學(xué)習(xí)之反射的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-01-01
  • SpringBoot使用PageHelper分頁詳解

    SpringBoot使用PageHelper分頁詳解

    這篇文章主要介紹了SpringBoot使用PageHelper分頁詳解,我們在任何的系統(tǒng)中,分頁功能是必不可少的,然而,對于這個功能如果有一種快速開發(fā)的實現(xiàn)方式,當然可以節(jié)省我們很多的時間了,接下來,我就給大家基于不同的環(huán)境來說說如何使用一個分頁插件,需要的朋友可以參考下
    2023-10-10
  • springmvc 傳遞和接收數(shù)組參數(shù)的實例

    springmvc 傳遞和接收數(shù)組參數(shù)的實例

    下面小編就為大家分享一篇springmvc 傳遞和接收數(shù)組參數(shù)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • Java 判斷數(shù)組是否相等的方法示例

    Java 判斷數(shù)組是否相等的方法示例

    這篇文章主要介紹了Java 判斷數(shù)組是否相等的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 詳解Mybatis核心配置文件

    詳解Mybatis核心配置文件

    今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識,文章圍繞著Mybatis核心配置文件展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • RabbitMQ使用SpringAMQP的配置方法

    RabbitMQ使用SpringAMQP的配置方法

    這篇文章主要介紹了RabbitMQ使用SpringAMQP的配置方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-03-03

最新評論

乐至县| 双柏县| 桐乡市| 仁怀市| 东丰县| 衡水市| 涟水县| 额尔古纳市| 星子县| 双城市| 饶平县| 菏泽市| 东乡| 东乡| 长宁区| 正宁县| 宜丰县| 尉氏县| 湖州市| 克东县| 开封市| 剑川县| 秦安县| 定兴县| 都安| 西青区| 孟州市| 南川市| 繁峙县| 新安县| 密山市| 三原县| 平山县| 景洪市| 北京市| 文水县| 绥江县| 临朐县| 凤阳县| 子长县| 姜堰市|