当我运行服务器和客户端..我无法在我的Android上的SD卡上找到图像...我需要帮助的人
我正在使用android studio和netbeans 强调文字
服务器java代码
package javasever;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
/**
*
* @author METALHEADi
*/
public class JavaSever {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Thread t= new Thread(new Runnable() {
@Override
public void run() {
try {
ServerSocket ss=new ServerSocket(400);
System.out.println("Runnig ........");
while(true){
Socket s=ss.accept();
System.out.println("Accept........");
FileInputStream fis =new FileInputStream("D:\\PIC\\all pic\\simo.png");
byte[]buffer=new byte [fis.available()];
fis.read(buffer);
ObjectOutputStream oos =new ObjectOutputStream(s.getOutputStream());
oos.write(buffer);
oos.close();
s.close();
}
} catch (IOException ex) {
Logger.getLogger(JavaSever.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
t.start();
}
}
客户端安卓代码
package com.example.metalheadi.androidclient;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
EditText IP,Port,Msg;
Button Send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Send= (Button) findViewById(R.id.send);
IP= (EditText) findViewById(R.id.ip_e);
Port=(EditText)findViewById(R.id.port_e);
Msg=(EditText)findViewById(R.id.msg_e);
Send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Thread thread =new Thread(new Runnable() {
@Override
public void run() {
try {
Socket socket= new Socket(IP.getText().toString(), Integer.parseInt(Port.getText().toString()));
// DataOutputStream stream =new DataOutputStream(socket.getOutputStream());
// stream.writeUTF(Msg.getText().toString());
// stream.flush();
// stream.close();
ObjectInputStream objectInputStream =new ObjectInputStream(socket.getInputStream());
try {
byte[] buffer = (byte[]) objectInputStream.readObject();
FileOutputStream fileOutputStream = new FileOutputStream("sdcard/simo.png");
fileOutputStream.write(buffer);
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();
}
});
}
}
XML android
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.metalheadi.androidclient.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="IP"
android:id="@+id/ip"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="PORT"
android:id="@+id/port"
android:layout_marginTop="51dp"
android:layout_below="@+id/ip"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Message"
android:id="@+id/msg"
android:layout_below="@+id/port"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="51dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ip_e"
android:layout_below="@+id/ip"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/port_e"
android:layout_below="@+id/port"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/ip_e"
android:layout_alignEnd="@+id/ip_e" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/msg_e"
android:layout_below="@+id/msg"
android:layout_alignRight="@+id/port_e"
android:layout_alignEnd="@+id/port_e"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SEND"
android:id="@+id/send"
android:layout_below="@+id/msg_e"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp" />
</RelativeLayout>
答案 0 :(得分:0)
你在这里弄错了。
available()
。请参阅Javadoc了解其真正的用途,并针对您使用它的方式发出特定警告。read()
返回的计数。您需要的只是以下复制循环,没有对象流:
byte[] buffer = new byte[8192];
int count;
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
两端都使用此功能。