我正在开发一个应用程序,在该应用程序中我以字符串格式向服务器发送数据。下面是我的代码。它工作正常。但现在我的问题是如何将整个 .txt 文件和 .doc 文件发送到服务器。在我的 GUI 中,我必须提供用户选择的路径。意味着用户必须选择他/她要发送的文件路径...请帮我解决我的问题...谢谢...
activity_main.xml 文件
<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"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="170sp"
android:layout_height="40sp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="@string/ip" />
<EditText
android:id="@+id/editText2"
android:layout_width="100sp"
android:layout_height="40sp"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_marginLeft="24dp"
android:layout_toRightOf="@+id/editText1"
android:ems="10"
android:hint="@string/port"
android:inputType="number" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText3"
android:layout_width="300sp"
android:layout_height="200sp"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:ems="10"
android:hint="@string/msg" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="64dp"
android:text="@string/send" />
</RelativeLayout>
.java 文件
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private Socket client;
private PrintWriter printwriter;
private EditText etMsg, etIP, etPort;
private Button button;
private String messsage;
int port = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etIP = (EditText) findViewById(R.id.editText1);
etPort = (EditText) findViewById(R.id.editText2);
etMsg = (EditText) findViewById(R.id.editText3);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
messsage = etMsg.getText().toString();
etMsg.setText("");
port = Integer.parseInt(etPort.getText().toString());
new Thread(new Runnable()
{
@Override
public void run() {
// TODO Auto-generated method stub
try
{
client = new Socket(etIP.getText().toString(), port);
printwriter = new PrintWriter(client.getOutputStream(),true);
printwriter.write(messsage);
printwriter.flush();
printwriter.close();
client.close();
}
catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
});
}
}
<uses-permission android:name="android.permission.INTERNET" />