简体   繁体   English

Erlang:发送文件和文件名

[英]Erlang: send file and filename

I'm interesting to send a file and it's filename.我很感兴趣发送一个文件,它是文件名。 Server's options:服务器选项:

-define(TCP_OPTIONS_SERVER, [binary, {packet, 0}, {active, false}]).

That's the receiver loop:那是接收器循环:

file_receiver_loop(Socket,Bs)->
case gen_tcp:recv(Socket, 0) of
    {ok, B} ->
        file_receiver_loop(Socket, [Bs, B]);
    {error, closed} ->
        io:format("~nReceived!~n ~p",[Bs]),
        save_file(Bs)
end.

I send file with:我发送文件:

file:sendfile(FilePath, Socket),

When I send file and filename当我发送文件和文件名时

gen_tcp:send(Socket,Filename),
file:sendfile(FilePath, Socket),

The Binary Data has a variable structure.二进制数据具有可变结构。

Thanks all!谢谢大家!

I make this code to solve the problem.我制作了这段代码来解决这个问题。
I send 30 byte for the file name.我发送 30 字节的文件名。
If the filename<30 I use the padding with white char.如果文件名<30,我使用带有白色字符的填充。
When the connection is accepted I call function file_name_receiver(Socket), that receive the filename:当连接被接受时,我调用函数 file_name_receiver(Socket),它接收文件名:

file_name_receiver(Socket)->
    {ok,FilenameBinaryPadding}=gen_tcp:recv(Socket,30),
    FilenamePadding=erlang:binary_to_list(FilenameBinaryPadding),
    Filename = string:strip(FilenamePadding,both,$ ),
    file_receiver_loop(Socket,Filename,[]).

This function riceive the binary data file:该函数生成二进制数据文件:

file_receiver_loop(Socket,Filename,Bs)->
    io:format("~nRicezione file in corso~n"),
    case gen_tcp:recv(Socket, 0) of
    {ok, B} ->
        file_receiver_loop(Socket, Filename,[Bs, B]);
    {error, closed} ->
        save_file(Filename,Bs)
end.

Finally, this function save the file.最后,这个函数保存文件。

%%salva il file
save_file(Filename,Bs) ->
    io:format("~nFilename: ~p",[Filename]),
    {ok, Fd} = file:open("../script/"++Filename, write),
    file:write(Fd, Bs),
    file:close(Fd).

The sender use a simple function:发件人使用一个简单的函数:

%%Permette l'invio di un file specificando host,filename e path assoluto
send_file(Host,Filename,FilePath,Port)->
    {ok, Socket} = gen_tcp:connect(list_to_atom(Hostname), Port,          TCP_OPTIONS_CLIENT),
    FilenamePadding = string:left(Filename, 30, $ ), %%Padding with white space
    gen_tcp:send(Socket,FilenamePadding),
    Ret=file:sendfile(FilePath, Socket),
    ok = gen_tcp:close(Socket).

I have made two function where you can send binary files with message passing.我做了两个函数,您可以在其中发送带有消息传递的二进制文件。

%Zip a file or a folder
send_zip(Pid, FolderName) ->
    %Name of you zip file
    FolderNameZip= FolderName++".zip",   
    %Zip the folder/file from the computer and name the zip file by FornderNameZip                    
    {ok, ZipFile} = zip:create(FolderNameZip, [FolderName]),
    %Put the Zipfile in a variable 
    {ok, ZipHandle} = file:read_file(ZipFile),
    %Send the zipfile to the other PID, sends the zipfile and his name to name it the same way.
    Pid ! {finish, ZipHandle, FolderNameZip}.


%save and unzip the in the computer 
register_zip_bash(Dir, ZipHandle, FolderNameZip) -> 
    file:write_file(FolderNameZip, ZipHandle),
    Cmd2 = io_lib:format("unzip -o -j ~p -d ~p/",[FolderNameZip, Dir]),
    _=os:cmd(Cmd2).

FoldeName can be the name of your file or your folder in your computer. FoldeName 可以是您的文件或计算机中文件夹的名称。 Pid is the persone who you would like to send the message. Pid 是您想要发送消息的人。

When you receive the ZipHandle, and the FolderNameZip use the register_zip_bash(...) function to save the data in your computer.当您收到 ZipHandle 和 FolderNameZip 时,使用 register_zip_bash(...) 函数将数据保存在您的计算机中。 Dir is the directory of where you would like to save the data. Dir 是您要保存数据的目录。 ZipHandle is the binary file that you receive from the PID function. ZipHandle 是您从 PID 函数接收的二进制文件。 FolderNameZip is the name of where the ZipHandle will be saved in the computer. FolderNameZip 是 ZipHandle 将在计算机中保存的位置的名称。

As you can see, send_zip(...) uses a erlang function and register_zip_bash(...) uses a bash command.如您所见, send_zip(...) 使用 erlang 函数, register_zip_bash(...) 使用 bash 命令。 You DO NOT HAVE to zip file to send the data.您不必压缩文件来发送数据。 Don't hesitate to change the code to make it suitable for you.不要犹豫,更改代码以使其适合您。

Hope this will help, Cheers.希望这会有所帮助,干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM