我正在尝试将文件从我的PC发送到android 对于PC我正在使用Python套接字服务器代码,要求pc用户输入路径,然后将文件发送到android mobile
然后将所选文件的位置保存为文件名变量“C:/new_image.jpg”
直到现在我已经能够发送和接收文件(PC通过Android Hotspot连接)并在android上将其保存为“temp_file”
现在我想从服务器本身接收文件名及其扩展名 我试着做了
conn.send(filename)
但是在android上我在Toast中收到空值
我是这个套接字编程的新手,抱歉编程不好
这是我的Python代码
import socket,thread
from collections import deque
from Tkinter import Tk
from tkFileDialog import askopenfilename
host=socket.gethostbyname(socket.gethostname())
port=54603
s=socket.socket()
s.bind((host,port))
s.listen(5)
Tk().withdraw()
filename = askopenfilename()
print filename
print "below s.listen %s" %host
def handleclient(conn):
f=open (filename, "rb")
l = f.read(1024)
while (l):
conn.send(l)
l = f.read(1024)
print "file has been sent! Now sending path of file"
conn.send(filename) #this is where null in android
print "sent path"
conn.close()
while True:
conn,addr=s.accept()
print 'connected to ',addr
thread.start_new_thread(handleclient,(conn,))
这是我的android接收代码
socket = new Socket(dstAddress, dstPort);
DataInputStream is = new DataInputStream(socket.getInputStream());
final File f = new File(Environment.getExternalStorageDirectory() + "/"
+ getPackageName() + "/temp_file"
);
File dirs = new File(f.getParent());
if (!dirs.exists())
dirs.mkdirs();
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte buf[] = new byte[10240];
int len=0;
while ((len = is.read(buf)) != -1)
{
bos.write(buf, 0, len);
bos.flush();
}
fos.close();
bos.close();
//TIll now received the file as temp_file but not extension
// Now next part i am trying to get extension
result11=is.readLine(); // here reading null
// result11=result11.substring(result11.lastIndexOf("/") + 1);
/* File from = new File(dirs,"temp_dhruvit");
File to = new File(dirs,result11);
if(from.exists())
from.renameTo(to);
d1.close();
is1.close(); */
is.close();
socket.close();