zoukankan      html  css  js  c++  java
  • 跨服务器上传文件方式

    跨服务器上传文件的方式有很多,其中一种是使用在中间服务器上使用临时文件的方式进行保存后再发送到另一个服务器上,实现文件上传。

    问题点:中间保存临时文件,还需要不定时的进行文件清理,比较麻烦

    直接进行文件的转发,使用byte[]数组方式直接进行文件转发,然后,服务器根据传递的byte[]数组进行转文件方式,使用httpclient方式将byte[]数组发送到服务端,特别注意的点在于,

    发送的时候使用"content-type" = "application/json"发送到服务端,服务端使用@RequestBody byte[] bytes 进行方法接收,其他的一些参数如果还要传递,可以使用header将参数传递过去

    package com.rainy.demo.utils;
    import
    org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; import org.apache.commons.httpclient.methods.PostMethod; import java.io.IOException; /** * Created by rainy on 2016/12/17. */ public class UpImgUtils { public static String getPath(String url, String fileType, byte[] bytes) { String path = ""; try { PostMethod method = new PostMethod(url); method.setRequestHeader("Content-type" , "application/json"); method.setRequestHeader("fileType", fileType); HttpClient httpClient = new HttpClient(); method.setRequestEntity(new ByteArrayRequestEntity(bytes)); int HttpCode = httpClient.executeMethod(method); if (HttpCode != HttpStatus.SC_OK) { throw new IOException("Invalid HttpStatus: " + HttpCode); } path = method.getResponseBodyAsString(); } catch (IOException e) { System.err.println(e.getMessage()); } return path; } }

    通常,SpringMVC 中使用 MultipartFile 进行文件上传,通过这样的方法获取到上传过来文件的byte[]数组

    @RequestMapping("/upload")
    @ResponseBody
    public String uploadFile(HttpServletRequest request){
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file");
        byte[] bytes = null;
        try {
            bytes = file.getBytes();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    服务器端使用,使用该方法来获取对应的上传文件byte[]数组

    public String upLoad(@RequestBody byte[] inputData, HttpServletRequest request) {
    
    }

    网友使用的byte[]数组转文件的方式,没有试过,不过这个不是重点,重点在于中间转发点,只要能拿到byte[]数组,转文件还是比较容易的

    public class T3 {  
      
        public static void main(String[] args){  
            String filePath = "E:\softoon\workspace_softoon\TestMobile\src\1.docx";  
            String outFilePath = "E:\softoon\workspace_softoon\TestMobile\src";  
            String outFileName = "2.docx";  
              
            getFile(getBytes(filePath),outFilePath,outFileName);  
        }  
      
        /** 
         * 获得指定文件的byte数组 
         */  
        public static byte[] getBytes(String filePath){  
            byte[] buffer = null;  
            try {  
                File file = new File(filePath);  
                FileInputStream fis = new FileInputStream(file);  
                ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);  
                byte[] b = new byte[1000];  
                int n;  
                while ((n = fis.read(b)) != -1) {  
                    bos.write(b, 0, n);  
                }  
                fis.close();  
                bos.close();  
                buffer = bos.toByteArray();  
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
            return buffer;  
        }  
      
        /** 
         * 根据byte数组,生成文件 
         */  
        public static void getFile(byte[] bfile, String filePath,String fileName) {  
            BufferedOutputStream bos = null;  
            FileOutputStream fos = null;  
            File file = null;  
            try {  
                File dir = new File(filePath);  
                if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在  
                    dir.mkdirs();  
                }  
                file = new File(filePath+"\"+fileName);  
                fos = new FileOutputStream(file);  
                bos = new BufferedOutputStream(fos);  
                bos.write(bfile);  
            } catch (Exception e) {  
                e.printStackTrace();  
            } finally {  
                if (bos != null) {  
                    try {  
                        bos.close();  
                    } catch (IOException e1) {  
                        e1.printStackTrace();  
                    }  
                }  
                if (fos != null) {  
                    try {  
                        fos.close();  
                    } catch (IOException e1) {  
                        e1.printStackTrace();  
                    }  
                }  
            }  
        }  
    }

    整体的使用方式已经写出来了,用在什么地方还需要讨论,不过,这样的实现方式还是很不错的,减少了临时文件的存放,而且,代码量减少很多。

  • 相关阅读:
    路由配置系统(views)
    Django-MTV简介
    web框架
    APScheduler
    python单例模式
    mysql_istall_db
    mysql多线程写入出现脏数据(重复数据)问题?
    南方周末 【1999年新年献词】总有一种力量让我们泪流满面
    卡特兰树通向证明
    可持久化线段树(主席树)快速简洁教程 图文并茂 保证学会。kth number例题
  • 原文地址:https://www.cnblogs.com/rainy-shurun/p/6193283.html
Copyright © 2011-2022 走看看