文件上传之跨服务器上传代码

文件上传之跨服务器上传代码

1.在index.jsp编写如下代码:

<%--
  Created by IntelliJ IDEA.
  User: Adair
  Date: 2020/7/8 0008
  Time: 10:18
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>文件上传</title>
</head>
<body>
    <h3>跨服务器文件上传</h3>
    <form action="/file/fileupload" method="post" enctype="multipart/form-data">
         选择文件:<input type="file" name="upload" /><br/>
        <input type="submit" value="上传" />
    </form>
</body>
</html>

2.创建文件上传控制器类控制器类的代码如下:

package com.txw.controller;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import java.util.UUID;
@Controller
@RequestMapping("/file")
public class FileController {
    
    
    /**
     * 跨服务器文件上传
     * @return
     */
    @RequestMapping("/fileupload")
    public String fileuoload(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;
        // 创建客户端的对象
        Client client = Client.create();
        // 和图片服务器进行连接
        WebResource webResource = client.resource(path + filename);
        // 上传文件
        webResource.put(upload.getBytes());
        return "success";
    }
}

3.需要用到依赖jar包的代码如下:

<?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.txw</groupId>
  <artifactId>springmvc_day02_02_fileupload</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>springmvc_day02_02_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.0.2.RELEASE</spring.version>
  </properties>
  <dependencies>
    <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.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-core</artifactId>
      <version>1.18.1</version>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-client</artifactId>
      <version>1.18.1</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>springmvc_day02_02_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.0.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.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.0</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>

4.使用flieupload服务运行结果如图所示:
在这里插入图片描述
5.使用Tomcat服务运行结果如图所示:
在这里插入图片描述
6.通过浏览器访问http://localhost:8080/如图所示:在这里插入图片描述
7.点击上传会跳转如图所示的界面:在这里插入图片描述
8.控制台打印结果如图所示:在这里插入图片描述
9.找到本地F:\workspace\fileuploadserver\target\fileuploadserver\uploads如图所示:在这里插入图片描述
10.如果出现如图所示的错误:
在这里插入图片描述
需要在D:\apache-tomcat-8.5.31\conf目录下web.xml如图所示:
在这里插入图片描述

打开web.xml找servlet并配置如图所示的代码:保存即可。
在这里插入图片描述
代码如下:

<init-param>
     <param-name>readonly</param-name>
     <param-value>false</param-value>
</init-param>

11.如果页面抛出404的错误,如图所示:
在这里插入图片描述
需要在另外一个存储文件的服务进行配置服务如图所示:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
重写部署项目就不会报以上的错误!

猜你喜欢

转载自blog.csdn.net/weixin_40055163/article/details/109215183
今日推荐