• springmvc中文件跨服务器传输的方法


     //1、首先在tomcat的新端口上重新开启一个tomcat服务器fileuploadserver服务器,并且在webapps下新建一个uploads文件夹

    //2、在业务服务器上书写前端页面和后端的方法

    // 注意:需要确定tomcat是否有写入权限如果没有需要修改tomcat的web.xml配置文件,赋予可写权限

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>Title</title>
    </head>
    <body>
    <h3>文件上传</h3>
    <%--<a href="user/fileUpload1">文件上传1</a>--%>
    <form action="/user/fileUpload1" method="post" enctype="multipart/form-data" >
    选择文件:<input type="file" name="upload"/><br/>
    <input type="submit" value="上传">
    </form>

    <form action="user/fileUpload2" method="post" enctype="multipart/form-data" >
    选择文件:<input type="file" name="upload"/><br/>
    <input type="submit" value="上传">
    </form>

    <h3>跨服务器文件上传</h3>

    <form action="user/fileupload3" method="post" enctype="multipart/form-data">
    选择文件:<input type="file" name="upload" /><br/>
    <input type="submit" value="上传" />
    </form>

    </body>
    </html>


    package com.hope.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.FileUploadException;
    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 org.springframework.web.multipart.MultipartResolver;

    import javax.servlet.http.HttpServletRequest;
    import java.io.File;
    import java.util.List;
    import java.util.UUID;

    /**
    * @author newcityman
    * @date 2019/11/28 - 13:04
    */
    @Controller("userController")
    @RequestMapping(path = "/user")
    public class UserController {
    /**
    * 传统方式上传
    *
    * @param request
    * @return
    * @throws Exception
    */
    @RequestMapping(path = "/fileUpload1")
    public String fileUpload1(HttpServletRequest request) throws Exception {
    System.out.println("文件上传。。。。");
    //使用fileupload组件完成文件上传
    //获取上传的位置
    String path = request.getSession().getServletContext().getRealPath("/uploads/");
    //判断,路径是否存在
    File file = new File(path);
    if (!file.exists()) {
    //创建该文件夹
    file.mkdirs();
    }
    //解析request对象,获取上传文件项
    DiskFileItemFactory factory = new DiskFileItemFactory(); //磁盘文件项工厂
    ServletFileUpload upload = new ServletFileUpload(factory);

    //解析rquest
    List<FileItem> items = upload.parseRequest(request);
    for (FileItem item : items) {
    //判断当前文件是否是上传文件项
    if (item.isFormField()) {
    //说明文件是普通文件项
    } else {
    //说明是上传文件项
    //获取上传文件的名称
    String fileName = item.getName();
    String uuid = UUID.randomUUID().toString().replace("-", "");
    fileName = uuid + "_" + fileName;
    //完成文件上传
    item.write(new File(path, fileName));
    //删除临时文件
    item.delete();
    }
    }
    return "success";
    }

    /**
    * 使用配置文件解析器的方式上传
    *
    * @param request
    * @param upload
    * @return
    * @throws Exception
    */

    @RequestMapping(path = "/fileUpload2")
    public String fileUpload2(HttpServletRequest request, MultipartFile upload) throws Exception {
    System.out.println("配置文件解析器的方式上传。。。。");
    //使用fileupload组件完成文件上传
    //获取上传的位置
    String path = request.getSession().getServletContext().getRealPath("/uploads/");
    //判断,路径是否存在
    File file = new File(path);
    if (!file.exists()) {
    //创建该文件夹
    file.mkdirs();
    }
    //获取上传文件的名称
    String filename = upload.getOriginalFilename();
    String uuid = UUID.randomUUID().toString().replace("-", "");
    filename = uuid + "_" + filename;
    //完成文件上传
    upload.transferTo(new File(path, filename));

    return "success";
    }

    /**
    * 跨服务器文件上传
    *
    * @param
    * @return
    * @throws Exception
    *//*

    @RequestMapping(path = "/fileUpload3")
    public String fileUpload3(MultipartFile upload) throws Exception {
    System.out.println("跨服务器的文件上传。。。。");
    //定义上传文件服务器路径
    String path = "http://localhost:9090/uploads/";

    //获取上传文件的名称
    String filename = upload.getOriginalFilename();
    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";
    }*/

    /**
    * 跨服务器文件上传
    * @return
    */
    @RequestMapping("/fileupload3")
    public String fileuoload3(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";
    }

    }
  • 相关阅读:
    mysql中drop、delete、truncate的区别简述
    hadoop之数据倾斜
    Mysql相关:navicat for mysql 加注释
    泛型
    工银亚洲见证开户详细过程和攻略
    classpath:和classpath*:的区别
    单索引与唯一索引
    MySQL中SQL语句之反引号,单引号
    《集体智慧编程》学习笔记 第三章
    《集体智慧编程》 读书笔记 第二章
  • 原文地址:https://www.cnblogs.com/newcityboy/p/11954077.html
Copyright © 2020-2023  润新知