zoukankan      html  css  js  c++  java
  • url转变为 MultipartFile对象

    /**
    * url转变为 MultipartFile对象
    * @param url
    * @param fileName
    * @return
    * @throws Exception
    */
    private static MultipartFile createFileItem(String url, String fileName) throws Exception{
    FileItem item = null;
    try {
    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    conn.setReadTimeout(30000);
    conn.setConnectTimeout(30000);
    //设置应用程序要从网络连接读取数据
    conn.setDoInput(true);
    conn.setRequestMethod("GET");
    if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
    InputStream is = conn.getInputStream();

    FileItemFactory factory = new DiskFileItemFactory(16, null);
    String textFieldName = "uploadfile";
    item = factory.createItem(textFieldName, ContentType.APPLICATION_OCTET_STREAM.toString(), false, fileName);
    OutputStream os = item.getOutputStream();

    int bytesRead = 0;
    byte[] buffer = new byte[8192];
    while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
    os.write(buffer, 0, bytesRead);
    }
    os.close();
    is.close();
    }
    } catch (IOException e) {
    throw new RuntimeException("文件下载失败", e);
    }

    return new CommonsMultipartFile(item);
    }
  • 相关阅读:
    maven配置
    redis测试
    智慧社区技术总结
    视频导航
    Delphi 任务栏中不显示窗口
    Delphi 设置程序图标为系统默认图标
    清除Windows系统图标缓存
    C/C++ 变量的本质分析
    005 C/C++ 数据类型_void
    004 C/C++ 数据类型_类型别名
  • 原文地址:https://www.cnblogs.com/lovedaodao/p/11271203.html
Copyright © 2011-2022 走看看