Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android PC文件传输:无法发送超过x字节的文件_Java_Android_Networking_File Transfer - Fatal编程技术网

Java Android PC文件传输:无法发送超过x字节的文件

Java Android PC文件传输:无法发送超过x字节的文件,java,android,networking,file-transfer,Java,Android,Networking,File Transfer,我正在构建一个应用程序,从Android手机或平板电脑向运行Java的计算机发送一个类似图片的文件。我的程序确实有效,但不是它应该的方式 我的意思是,我希望在度假时能够将未压缩的文件从手机发送到桌面。我的手机图片每个大约7兆字节。我的程序能够发送一个22千字节的文件,发送的次数是我想要的次数,但是任何大于这个数目的文件都会失败。我想这可能是我的缓冲区的问题,但我不确定。然而,发送一个大的文本文件让我相信我的大文件在传输过程中会被破坏,因为PC上显示的文件大小正确,只是无法打开 服务器(PC)的代

我正在构建一个应用程序,从Android手机或平板电脑向运行Java的计算机发送一个类似图片的文件。我的程序确实有效,但不是它应该的方式

我的意思是,我希望在度假时能够将未压缩的文件从手机发送到桌面。我的手机图片每个大约7兆字节。我的程序能够发送一个22千字节的文件,发送的次数是我想要的次数,但是任何大于这个数目的文件都会失败。我想这可能是我的缓冲区的问题,但我不确定。然而,发送一个大的文本文件让我相信我的大文件在传输过程中会被破坏,因为PC上显示的文件大小正确,只是无法打开

服务器(PC)的代码:

以及应用程序(Android)的代码:

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button button;
    public String IP;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.sendB);
        button.setOnClickListener(this);
    }

    public void onClick(View v) {
        EditText et = (EditText) findViewById(R.id.editText3);
        IP = et.getText().toString();
        Intent i;
        i = new Intent(this, OpenFileActivity.class);
        this.setContentView(R.layout.activity_open_file);
        this.startActivityForResult(i, 2);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String fileName = null;
        if (resultCode == 2) {
            fileName = data.getStringExtra("fileName");
        } else {
            Toast.makeText(this,"No File Selected, Cancel Or Back Pressed",Toast.LENGTH_SHORT).show();
        }
        sendTheFile ooh = new sendTheFile();
        ooh.execute(fileName,IP);
    }
}

class sendTheFile extends AsyncTask<String, Void, String> {
    public OutputStream out = null;
    public Socket socket = null;
    public File myFile = null;
    public byte[] buffer;
    @Override
    protected String doInBackground(String[] params) {
        String IP = params[1];
        myFile = new File(params[0]);
        try {
            socket = new Socket(IP, 25000);
            FileInputStream fis = new FileInputStream(myFile);
            buffer = new byte[(int) myFile.length()];
            BufferedInputStream in = new BufferedInputStream(fis);
            in.read(buffer, 0, buffer.length);
            out = socket.getOutputStream();
            out.write(buffer,0,buffer.length);
            out.flush();
            out.close();
            in.close();
            socket.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            Log.i("TAG","Failed to send file");
        }
        return "";
    }
}
public class OpenFileActivity extends Activity
    implements OnClickListener, OnItemClickListener {
    ListView LvList;
    ArrayList<String> listItems = new ArrayList<String>();

    ArrayAdapter<String> adapter;

    Button BtnOK;
    Button BtnCancel;

    String currentPath = null;

    public String selectedFilePath = null;
    String selectedFileName = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_open_file);

        try {
            LvList = (ListView) findViewById(R.id.LvList);
            BtnOK = (Button) findViewById(R.id.BtnOK);
            BtnCancel = (Button) findViewById(R.id.BtnCancel);
            LvList.setOnItemClickListener(this);

            BtnOK.setOnClickListener(this);
            BtnCancel.setOnClickListener(this);




  setCurrentPath(Environment.getExternalStorageDirectory().getAbsolutePath() + "/");
        } catch (Exception ex) {
            Toast.makeText(this,
                    "Error in OpenFileActivity.onCreate: " + ex.getMessage(),
                    Toast.LENGTH_SHORT).show();
        }
    }

    void setCurrentPath(String path) {
        ArrayList<String> folders = new ArrayList<String>();

        ArrayList<String> files = new ArrayList<String>();

        currentPath = path;

        File[] allEntries = new File(path).listFiles();

        for (int i = 0; i < allEntries.length; i++) {
            if (allEntries[i].isDirectory()) {
                folders.add(allEntries[i].getName());
            } else if (allEntries[i].isFile()) {
                files.add(allEntries[i].getName());
            }
        }

        Collections.sort(folders, new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                return s1.compareToIgnoreCase(s2);
            }
        });

        Collections.sort(files, new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                return s1.compareToIgnoreCase(s2);
            }
        });

        listItems.clear();

        for (int i = 0; i < folders.size(); i++) {
            listItems.add(folders.get(i) + "/");
        }

        for (int i = 0; i < files.size(); i++) {
            listItems.add(files.get(i));
        }

        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                listItems);
        adapter.notifyDataSetChanged();

        LvList.setAdapter(adapter);
    }

    @Override
    public void onBackPressed()
    {
        if (!currentPath.equals(Environment.getExternalStorageDirectory().getAbsolutePath() + "/")) {
            setCurrentPath(new File(currentPath).getParent() + "/");
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        //Toast.makeText(this, v.getTag().toString(),Toast.LENGTH_SHORT).show();
        //switch (v.getId()) {
        //case R.id.BtnOK:

        intent = new Intent();
        intent.putExtra("fileName", selectedFilePath);
        intent.putExtra("shortFileName", selectedFileName);
        setResult(2, intent);

        this.finish();

    }
}
public类MainActivity扩展AppCompatActivity实现View.OnClickListener{
按钮;
公共字符串IP;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
按钮=(按钮)findViewById(R.id.sendB);
setOnClickListener(此);
}
公共void onClick(视图v){
EditText et=(EditText)findViewById(R.id.editText3);
IP=et.getText().toString();
意图一;
i=新的意图(这个,OpenFileActivity.class);
这个.setContentView(R.layout.activity\u open\u文件);
这是startActivityForResult(i,2);
}
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
字符串文件名=null;
如果(结果代码==2){
fileName=data.getStringExtra(“文件名”);
}否则{
Toast.makeText(这是“未选择文件、取消或反按”,Toast.LENGTH_SHORT.show();
}
sendTheFile ooh=新的sendTheFile();
执行(文件名,IP);
}
}
类sendTheFile扩展异步任务{
public OutputStream out=null;
公共套接字=空;
公共文件myFile=null;
公共字节[]缓冲区;
@凌驾
受保护的字符串doInBackground(字符串[]参数){
字符串IP=参数[1];
myFile=新文件(参数[0]);
试一试{
插座=新插座(IP,25000);
FileInputStream fis=新的FileInputStream(myFile);
buffer=新字节[(int)myFile.length()];
BufferedInputStream in=新的BufferedInputStream(fis);
in.read(buffer,0,buffer.length);
out=socket.getOutputStream();
out.write(buffer,0,buffer.length);
out.flush();
out.close();
in.close();
socket.close();
}捕获(例外情况除外){
例如printStackTrace();
Log.i(“标记”,“发送文件失败”);
}
返回“”;
}
}
OpenFileActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button button;
    public String IP;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.sendB);
        button.setOnClickListener(this);
    }

    public void onClick(View v) {
        EditText et = (EditText) findViewById(R.id.editText3);
        IP = et.getText().toString();
        Intent i;
        i = new Intent(this, OpenFileActivity.class);
        this.setContentView(R.layout.activity_open_file);
        this.startActivityForResult(i, 2);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String fileName = null;
        if (resultCode == 2) {
            fileName = data.getStringExtra("fileName");
        } else {
            Toast.makeText(this,"No File Selected, Cancel Or Back Pressed",Toast.LENGTH_SHORT).show();
        }
        sendTheFile ooh = new sendTheFile();
        ooh.execute(fileName,IP);
    }
}

class sendTheFile extends AsyncTask<String, Void, String> {
    public OutputStream out = null;
    public Socket socket = null;
    public File myFile = null;
    public byte[] buffer;
    @Override
    protected String doInBackground(String[] params) {
        String IP = params[1];
        myFile = new File(params[0]);
        try {
            socket = new Socket(IP, 25000);
            FileInputStream fis = new FileInputStream(myFile);
            buffer = new byte[(int) myFile.length()];
            BufferedInputStream in = new BufferedInputStream(fis);
            in.read(buffer, 0, buffer.length);
            out = socket.getOutputStream();
            out.write(buffer,0,buffer.length);
            out.flush();
            out.close();
            in.close();
            socket.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            Log.i("TAG","Failed to send file");
        }
        return "";
    }
}
public class OpenFileActivity extends Activity
    implements OnClickListener, OnItemClickListener {
    ListView LvList;
    ArrayList<String> listItems = new ArrayList<String>();

    ArrayAdapter<String> adapter;

    Button BtnOK;
    Button BtnCancel;

    String currentPath = null;

    public String selectedFilePath = null;
    String selectedFileName = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_open_file);

        try {
            LvList = (ListView) findViewById(R.id.LvList);
            BtnOK = (Button) findViewById(R.id.BtnOK);
            BtnCancel = (Button) findViewById(R.id.BtnCancel);
            LvList.setOnItemClickListener(this);

            BtnOK.setOnClickListener(this);
            BtnCancel.setOnClickListener(this);




  setCurrentPath(Environment.getExternalStorageDirectory().getAbsolutePath() + "/");
        } catch (Exception ex) {
            Toast.makeText(this,
                    "Error in OpenFileActivity.onCreate: " + ex.getMessage(),
                    Toast.LENGTH_SHORT).show();
        }
    }

    void setCurrentPath(String path) {
        ArrayList<String> folders = new ArrayList<String>();

        ArrayList<String> files = new ArrayList<String>();

        currentPath = path;

        File[] allEntries = new File(path).listFiles();

        for (int i = 0; i < allEntries.length; i++) {
            if (allEntries[i].isDirectory()) {
                folders.add(allEntries[i].getName());
            } else if (allEntries[i].isFile()) {
                files.add(allEntries[i].getName());
            }
        }

        Collections.sort(folders, new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                return s1.compareToIgnoreCase(s2);
            }
        });

        Collections.sort(files, new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                return s1.compareToIgnoreCase(s2);
            }
        });

        listItems.clear();

        for (int i = 0; i < folders.size(); i++) {
            listItems.add(folders.get(i) + "/");
        }

        for (int i = 0; i < files.size(); i++) {
            listItems.add(files.get(i));
        }

        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                listItems);
        adapter.notifyDataSetChanged();

        LvList.setAdapter(adapter);
    }

    @Override
    public void onBackPressed()
    {
        if (!currentPath.equals(Environment.getExternalStorageDirectory().getAbsolutePath() + "/")) {
            setCurrentPath(new File(currentPath).getParent() + "/");
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        //Toast.makeText(this, v.getTag().toString(),Toast.LENGTH_SHORT).show();
        //switch (v.getId()) {
        //case R.id.BtnOK:

        intent = new Intent();
        intent.putExtra("fileName", selectedFilePath);
        intent.putExtra("shortFileName", selectedFileName);
        setResult(2, intent);

        this.finish();

    }
}
公共类OpenFileActivity扩展活动
实现OnClickListener、OnItemClickListener{
列表视图列表;
ArrayList listItems=新的ArrayList();
阵列适配器;
按钮BtnOK;
按钮BtnCancel;
字符串currentPath=null;
公共字符串selectedFilePath=null;
字符串selectedFileName=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u open\u文件);
试一试{
LvList=(ListView)findViewById(R.id.LvList);
BtnOK=(按钮)findviewbyd(R.id.BtnOK);
BtnCancel=(按钮)findViewById(R.id.BtnCancel);
LvList.setOnItemClickListener(this);
BtnOK.setOnClickListener(这个);
BtnCancel.setOnClickListener(此);
setCurrentPath(Environment.getExternalStorageDirectory().getAbsolutePath()+“/”;
}捕获(例外情况除外){
Toast.makeText(这个,
OpenFileActivity.onCreate中出错:“+ex.getMessage(),
吐司。长度(短)。show();
}
}
void setCurrentPath(字符串路径){
ArrayList folders=新建ArrayList();
ArrayList files=新的ArrayList();
currentPath=路径;
File[]allEntries=新文件(路径).listFiles();
for(int i=0;i