JAVA读取网络文件流并获取文件名称并转换为MultipartFile类型

 1.调用

   Map inputStreamByUrl = FileUtils.getInputStreamByUrl(url);
   MultipartFile file = FileUtils.getMultipartFile((InputStream)inputStreamByUrl.get("file"),inputStreamByUrl.get("fileName").toString());

2.方法

    /**
     * 根据地址获得数据的输入流
     * @param strUrl 网络连接地址
     * @return url的输入流
     */
    public static Map getInputStreamByUrl(String strUrl){
        Map inputMap = new HashMap<>();
        HttpURLConnection conn = null;
        try {
            URL url = new URL(strUrl);
            conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(20 * 1000);
            String headerField = conn.getHeaderField("Content-Disposition");
            headerField = new String(headerField.getBytes("GB2312"));
            System.out.println(headerField);
            headerField = new String(headerField.getBytes("utf-8"));
            System.out.println(headerField);

            if (headerField == null||!headerField.contains("filename=\"")){
                inputMap.put("fileName", UUID.randomUUID().toString());
            }else {
                inputMap.put("fileName", headerField.substring(headerField.lastIndexOf("fileName=")+9));
            }

            final ByteArrayOutputStream output = new ByteArrayOutputStream();
            IOUtils.copy(conn.getInputStream(),output);
            inputMap.put("file",new ByteArrayInputStream(output.toByteArray()))
            return inputMap;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try{
                if (conn != null) {
                    conn.disconnect();
                }
            }catch (Exception e){
            }
        }
        return null;
    }
    /**
     * 获取封装得MultipartFile
     *
     * @param inputStream inputStream
     * @return MultipartFile
     */
    public static MultipartFile getMultipartFile(InputStream inputStream,String fileName) {
        FileItem fileItem = createFileItem(inputStream, fileName);
        //CommonsMultipartFile是feign对multipartFile的封装,但是要FileItem类对象
        return new CommonsMultipartFile(fileItem);
    }
 /**
     * FileItem类对象创建
     *
     * @param inputStream inputStream
     * @param fileName    fileName
     * @return FileItem
     */
    public static FileItem createFileItem(InputStream inputStream, String fileName) {
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        String textFieldName = "file";
        FileItem item = factory.createItem(textFieldName, MediaType.MULTIPART_FORM_DATA_VALUE, true, fileName);
        int bytesRead = 0;
        byte[] buffer = new byte[10 * 1024 * 1024];
        OutputStream os = null;
        //使用输出流输出输入流的字节
        try {
            os = item.getOutputStream();
            while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            inputStream.close();
        } catch (IOException e) {
            throw new IllegalArgumentException("文件上传失败");
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {

                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                }
            }
        }

        return item;
    }

你可能感兴趣的:(java,java)