<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                一.**pom.xml**代碼 ~~~ <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.youge</groupId> <artifactId>springmvc_fileupload</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>springmvc_fileupload Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring.version>5.2.9.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <!-- SpringMVC方式文件上傳包--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> <!-- SpringMVC方式文件上傳包--> <!-- 跨服務器文件上傳包--> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> <version>1.19.4</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.19.4</version> </dependency> <!-- 跨服務器文件上傳包--> </dependencies> <build> <finalName>springmvc_fileupload</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project> ~~~ ***** 二.**web.xml** 代碼 ~~~ <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <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> <!-- 配置解決中文亂碼過濾器--> <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> ~~~ ***** 三.**springmvc.xml** 代碼 ~~~ <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 開啟注解掃描--> <context:component-scan base-package="com.youge"></context:component-scan> <!-- 視圖解析器對象--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 配置自定義類型轉換器--> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="com.youge.utils.StringToDateConverter"/> </set> </property> </bean> <!-- 配置文件解析器對象--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10485760"/> </bean> <!-- 前端控制器,哪些靜態資源不攔截--> <mvc:resources mapping="/js/**" location="/js/"></mvc:resources> <mvc:resources mapping="/css/**" location="/css/"></mvc:resources> <mvc:resources mapping="/images/**" location="/images/"></mvc:resources> <!-- 開啟SpringMVC框架注解的支持--> <mvc:annotation-driven conversion-service="conversionService"/> </beans> ~~~ ***** 四.**index.jsp** 代碼 ~~~ <%-- Created by IntelliJ IDEA. User: Administrator Date: 2020-09-27 Time: 10:13 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> <h2>傳統方式文件上傳</h2> <form action="/user/fileupload1" method="post" enctype="multipart/form-data"> 選擇文件:<input type="file" name="upload"><br> 選擇文件:<input type="file" name="upload"><br> 用戶名:<input type="text" name="name" /><br> <input type="submit" value="上傳"> </form> <h2>springmvc方式文件上傳</h2> <form action="/user/fileupload2" method="post" enctype="multipart/form-data"> 選擇文件:<input type="file" name="uploads"><br> 選擇文件:<input type="file" name="uploads"><br> 用戶名:<input type="text" name="name" /><br> <input type="submit" value="上傳"> </form> <h2>跨服務器文件上傳</h2> <form action="/user/fileupload3" method="post" enctype="multipart/form-data"> 選擇文件:<input type="file" name="upload"><br> <input type="submit" value="上傳"> </form> </body> </html> ~~~ ***** 五.**UserController.java**代碼 ~~~ package com.youge.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; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020-09-27 10:20 */ @Controller @RequestMapping("/user") public class UserController { /** * 傳統方式文件上傳 * * @return */ @RequestMapping("/fileupload1") public String fileupload1(HttpServletRequest request) throws Exception { System.out.println("傳統方式文件上傳...."); //使用fileupload組件完成文件上傳 //上傳位置 String path = request.getSession().getServletContext().getRealPath("/uploads/"); //判斷該路徑是否存在 File file = new File(path); // System.out.println(path); if (!file.exists()) { //創建該文件夾 // System.out.println(11111); 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(); //把文件的名稱設置唯一值,uuid String uuid = UUID.randomUUID().toString().replace("-", ""); fileName = uuid + "_" + fileName; System.out.println(fileName); //完成文件上傳 item.write(new File(path, fileName)); //刪除臨時文件 item.delete(); } } return "success"; } /** * SpringMVC方式文件上傳 * * @param request * @param uploads * @return * @throws Exception */ @RequestMapping("/fileupload2") public String fileupload2(HttpServletRequest request, MultipartFile[] uploads) throws Exception { System.out.println("SpringMVC方式文件上傳...."); //使用fileupload組件完成文件上傳 //上傳位置 String path = request.getSession().getServletContext().getRealPath("/uploads/"); //判斷該路徑是否存在 File file = new File(path); // System.out.println(path); if (!file.exists()) { //創建該文件夾 // System.out.println(11111); file.mkdirs(); } //獲取上傳文件的名稱 for (MultipartFile upload : uploads) { String fileName = upload.getOriginalFilename(); //把文件的名稱設置唯一值,uuid String uuid = UUID.randomUUID().toString().replace("-", ""); fileName = uuid + "_" + fileName; System.out.println(fileName); //完成文件上傳 upload.transferTo(new File(path, fileName)); } return "success"; } /** * 跨服務器文件上傳 * * @param upload * @return * @throws Exception */ @RequestMapping("/fileupload3") public String fileupload3(MultipartFile upload) throws Exception { System.out.println("跨服務器文件上傳...."); //定義上傳文件服務器路徑 String path = "http://localhost:9090/uploads/"; //獲取上傳文件的名稱 String fileName = upload.getOriginalFilename(); //把文件的名稱設置唯一值,uuid String uuid = UUID.randomUUID().toString().replace("-", ""); fileName = uuid + "_" + fileName; System.out.println(fileName); //完成文件上傳 跨服務大上傳 //1.創建客戶端的對象 Client client = Client.create(); //2.和圖片服務器進行連接 WebResource webResource = client.resource(path + fileName); System.out.println("webResource"); System.out.println(webResource); //3.上傳文件 webResource.put(upload.getBytes()); return "success"; } } ~~~ ***** 六.解決跨域問題 找到tomcat安裝目錄下的conf下的web.xml文件,找到如下圖所示的位置,加入框框的代碼 ![](https://img.kancloud.cn/0e/fc/0efc71171cabf0f9d4df453979fe2d52_889x435.png) ***** 備注: 1.建 **uploads**文件夾,位置如下圖 ![](https://img.kancloud.cn/0c/18/0c18d1c225d9d504a2222be5ccd8beb7_421x217.png) 2.創建 文件上傳服務器,只需簡單的創建就行,不需要引入jar包和配置文件
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看