String dest = "d:/download/" + name;

            Path path = Paths.get(dest).getParent().toAbsolutePath().normalize();

            if(!Files.exists(path))
            {
                try {
                    Files.createDirectories(path);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            try (FileChannel fc = new FileOutputStream(dest).getChannel()){

                ByteBuffer buffer = getResponseAttachment(url);
                fc.write(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }

 

import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
  public static void main(String[] argv) throws Exception {
    ByteBuffer bbuf = ByteBuffer.allocate(100);
    File file = new File("filename");

    boolean append = false;

    FileChannel wChannel = new FileOutputStream(file, append).getChannel();

    wChannel.write(bbuf);

    wChannel.close();
  }
}
String dest = "d:/download/" + name;
            try (FileChannel fc = FileChannel.open(Paths.get(dest), StandardOpenOption.WRITE)){
                ByteBuffer buffer = getResponseAttachment(url);
                fc.write(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }

 

相关文章:

  • 2022-01-07
  • 2021-08-14
  • 2022-12-23
  • 2022-12-23
  • 2021-09-14
  • 2021-08-28
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-10
  • 2022-12-23
  • 2022-12-23
  • 2021-12-04
相关资源
相似解决方案