小知识——java实现向目标服务器写入文件

  • Post author:
  • Post category:java


【本文将介绍如何用java实现向目标服务器共享文件夹写入文件】

目标服务器需先向本机共享一个可写入的文件夹。

/**
 * ip——1.1.1.1
 * adder——\\Users\\lxw\\Desktop\\a
 */
public int inputFile(String ip, String adder, String name, InputStream in){
    FileOutputStream tos = null;
    try{
        byte[] buffer = new byte[1024];
        String finalPathAndName = "\\\\"+ip+adder+"\\"+name;
        new File(finalPathAndName).createNewFile();
        tos = new FileOutputStream(new File(finalPathAndName));
        int len;
        while ((len = in.read(buffer)) > 0) {
            tos.write(buffer, 0, len);
        }
        in.close();
        tos.close();
        return 1;
    }catch(Exception e){
        return -1;
    }finally{
        if(in!=null){
            try {
                in.close();
            } catch (IOException e) {
            }
        }
        if(tos!=null){
            try {
                tos.close();
            } catch (IOException e) {
            }
        }
    }
}



版权声明:本文为lvxiaowei777原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。