客户机 - 服务器:通过接口从Android文件传输至PC连接客户机、文件传输、接口、服务器

2023-09-12 09:40:18 作者:此用户下落不明

我想从画廊我的手机发送图片,我选择,并发送照片从手机到我的电脑,当我点击该按钮,它应该采取图片的路径,并把它放在一个文件中,并将其传送到我的电脑。我检查我的电脑上的code - 双方 - 和它的工作完全罚款

当我测试它,什么都不会发生!

下面是code:

服务器端:

 进口java.io. *;
进口java.net *。

公共类文件服务器{

    公共静态无效的主要(字串[] args)抛出IOException异常{
        INT文件大小= 6022386; //文件大小暂难codeD

        长时间启动= System.currentTimeMillis的();
        INT读取动作;
        INT电流= 0;

        //创建套接字
        ServerSocket的servsock =新的ServerSocket(1149);
        而(真){
          的System.out.println(等待...);

          插座袜子= servsock.accept();
          的System.out.println(接受连接:+袜子);

       //接收文件
            byte []的mybytearray =新的字节[文件大小]
            InputStream的是= sock.getInputStream();
            FileOutputStream中FOS =新的FileOutputStream(C:\\用户\\ \\阿瓦文件\\ WebOffice.jpg); //目标路径和文件名
            的BufferedOutputStream BOS =新的BufferedOutputStream(FOS);
            读取动作= is.​​read(mybytearray,0,mybytearray.length);
            电流=读取动作;

            //感谢A.加的斯的bug修复
            做 {
               读取动作=
                  is.read(mybytearray,目前,(mybytearray.length流));
               如果(读取动作> = 0)电流+ =读取动作;
            }而(读取动作&-1)〜;

            bos.write(mybytearray,0,电流);
            bos.flush();
            长端= System.currentTimeMillis的();
            的System.out.println(年底启动);
            bos.close();



          sock.close();
          }
    }

}
 
vivox5如何连接电脑来传送文件

客户端(安卓):

 包com.arwa.file.send;

进口java.io.BufferedInputStream中;
进口的java.io.File;
进口java.io.FileInputStream中;
进口java.io.IOException异常;
进口java.io.ObjectOutputStream中;
进口java.io.OutputStream中;
进口的java.net.Socket;
进口的java.net.UnknownHostException;

进口android.app.Activity;
进口android.content.Intent;
进口android.database.Cursor;
进口android.net.Uri;
进口android.os.Bundle;
进口android.provider.MediaStore;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.ImageView;
进口android.widget.TextView;

公共类SendfileActivity延伸活动{
    / **第一次创建活动时调用。 * /

    私有静态最终诠释SELECT_PICTURE = 1;

    私人字符串selectedImagePath;
    私人ImageView的IMG;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        的System.out.println(34);
        IMG =(ImageView的)findViewById(R.id.ivPic);
        的System.out.println(36);
        ((按钮)findViewById(R.id.bBrowse))
                .setOnClickListener(新OnClickListener(){
                    公共无效的onClick(查看为arg0){
                        的System.out.println(40);
                        意向意图=新的意图();
                        intent.setType(图像/ *);
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(
                                Intent.createChooser(意向,选择图片),
                                SELECT_PICTURE);
                        的System.out.println(47);
                    }
                });
        ;
        的System.out.println(51);
        按钮发送=(按钮)findViewById(R.id.bSend);
        最后的TextView状态=(TextView中)findViewById(R.id.tvStatus);

        send.setOnClickListener(新View.OnClickListener(){

            @覆盖
            公共无效的onClick(查看为arg0){

                插座袜子;
                尝试 {
                    袜子=新的Socket(MY_PCs_IP,1149);
                    的System.out.println(连接...);

                     // 发送文件
                          myfile文件=新的文件(selectedImagePath);
                          byte []的mybytearray =新的字节[(INT)myFile.length()];
                          的FileInputStream FIS =新的FileInputStream(MYFILE);
                          的BufferedInputStream双=新的BufferedInputStream(FIS);
                          bis.read(mybytearray,0,mybytearray.length);
                          OutputStream的OS = sock.getOutputStream();
                          的System.out.println(发送...);
                          os.write(mybytearray,0,mybytearray.length);
                          os.flush();

                        sock.close();
                }赶上(UnknownHostException异常E){
                    // TODO自动生成的catch块
                    e.printStackTrace();
                }赶上(IOException异常E){
                    // TODO自动生成的catch块
                    e.printStackTrace();
                }



            }
        });
    }
    公共无效onActivityResult(INT申请code,INT结果code,意图数据){
        如果(结果code == RESULT_OK){
            如果(要求code == SELECT_PICTURE){
                乌里selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                TextView的路径=(TextView中)findViewById(R.id.tvPath);
                path.setText(映像路径:+ selectedImagePath);
                img.setImageURI(selectedImageUri);
            }
        }
    }

    公共字符串getPath(URI URI){
        的String []投影= {MediaStore.Images.Media.DATA};
        光标光标= managedQuery(URI,投影,NULL,NULL,NULL);
        INT与Column_Index =光标
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        返回cursor.getString(Column_Index中);
    }
}
 

XML code:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>

    <的TextView
        机器人:ID =@ + ID / tvStatus
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=@字符串/你好/>

    <的TextView
        机器人:ID =@ + ID / tvPath
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=路径:/>

    <按钮
        机器人:ID =@ + ID / bBrowse
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=浏览>
    < /按钮>

    <按钮
        机器人:ID =@ + ID / BSEND
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文=发送/>

    < ImageView的
        机器人:ID =@ + ID / ivPic
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT>
    < / ImageView的>

< / LinearLayout中>
 

解决方案

你刚才添加的权限

 <使用-权限的Andr​​oid:名称=android.permission.ACCESS_NETWORK_STATE/>
<使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
 

和它的工作完美。

I'm trying to send a picture I choose from the gallery on my phone, and send that picture from the phone to my PC, when I click on the button it should take the picture's path and put it in a file, and transfer it to my PC. I've checked the code on my PC - for both sides - and it worked totally fine.

When I test it out, nothing happens!

Here is the code:

Server side:

    import java.io.*;
import java.net.*;

public class FileServer {

    public static void main(String[] args) throws IOException {
        int filesize=6022386; // filesize temporary hardcoded

        long start = System.currentTimeMillis();
        int bytesRead;
        int current = 0;

        // create socket
        ServerSocket servsock = new ServerSocket(1149);
        while (true) {
          System.out.println("Waiting...");

          Socket sock = servsock.accept();
          System.out.println("Accepted connection : " + sock);

       // receive file
            byte [] mybytearray  = new byte [filesize];
            InputStream is = sock.getInputStream();
            FileOutputStream fos = new FileOutputStream("C:\\Users\\Arwa\\Documents\\WebOffice.jpg"); // destination path and name of file
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            bytesRead = is.read(mybytearray,0,mybytearray.length);
            current = bytesRead;

            // thanks to A. Cádiz for the bug fix
            do {
               bytesRead =
                  is.read(mybytearray, current, (mybytearray.length-current));
               if(bytesRead >= 0) current += bytesRead;
            } while(bytesRead > -1);

            bos.write(mybytearray, 0 , current);
            bos.flush();
            long end = System.currentTimeMillis();
            System.out.println(end-start);
            bos.close();



          sock.close();
          }
    }

}

Client side (Android):

 package com.arwa.file.send;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class SendfileActivity extends Activity {
    /** Called when the activity is first created. */

    private static final int SELECT_PICTURE = 1;

    private String selectedImagePath;
    private ImageView img;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        System.out.println("34");
        img = (ImageView) findViewById(R.id.ivPic);
        System.out.println("36");
        ((Button) findViewById(R.id.bBrowse))
                .setOnClickListener(new OnClickListener() {
                    public void onClick(View arg0) {
                        System.out.println("40");
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(
                                Intent.createChooser(intent, "Select Picture"),
                                SELECT_PICTURE);
                        System.out.println("47");
                    }
                });
        ;
        System.out.println("51");
        Button send = (Button) findViewById(R.id.bSend);
        final TextView status = (TextView) findViewById(R.id.tvStatus);

        send.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Socket sock;
                try {
                    sock = new Socket("MY_PCs_IP", 1149); 
                    System.out.println("Connecting...");

                     // sendfile
                          File myFile = new File (selectedImagePath); 
                          byte [] mybytearray  = new byte [(int)myFile.length()];
                          FileInputStream fis = new FileInputStream(myFile);
                          BufferedInputStream bis = new BufferedInputStream(fis);
                          bis.read(mybytearray,0,mybytearray.length);
                          OutputStream os = sock.getOutputStream();
                          System.out.println("Sending...");
                          os.write(mybytearray,0,mybytearray.length);
                          os.flush();

                        sock.close();
                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



            }
        });
    }
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                TextView path = (TextView) findViewById(R.id.tvPath);
                path.setText("Image Path : " + selectedImagePath);
                img.setImageURI(selectedImageUri);
            }
        }
    }

    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
}

XML code:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvStatus"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <TextView
        android:id="@+id/tvPath"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Path: " />

    <Button
        android:id="@+id/bBrowse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Browse" >
    </Button>

    <Button
        android:id="@+id/bSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send" />

    <ImageView
        android:id="@+id/ivPic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ImageView>

</LinearLayout>

解决方案

you have just to add the permission

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

and it will work perfect.