我正在构建一个应用程序,用于将Android手机或平板电脑等图片文件发送到运行Java的计算机。我的程序 工作,但不是应该的方式。
我的意思是我希望能够在度假时将未压缩的文件从手机发送到我的桌面。我的手机图片各约为7兆字节。我的程序可以根据需要多次发送22千字节的文件,但任何大于该文件的文件都会失败。我认为这可能与我的缓冲区有关,但我不确定。但是,发送大文本文件会让我相信我的大文件在传输过程中会被损坏,因为PC上显示的文件大小合适,只是不可打开。
服务器代码(PC):
public class AEFT implements ActionListener {
public ServerSocket server = null;
public boolean server_up = false;
public Socket socket = null;
public int maxsize = 999999999;
public String file_name = "file.txt";
public int byteread, current;
public JTextField fileTF;
public void start_server() {
String start_fail = "ERROR: Failed to start server";
if(!server_up) {
try {
server = new ServerSocket(25000);
server_up = true;
System.out.println("INFO: Server started successfully.");
wait_for_file();
}catch(Exception ex) {
JOptionPane.showMessageDialog(null, "ERROR: Failed to start server.");
System.out.println(start_fail + ": Unknown error occured.");
}
}
else {
System.out.println(start_fail + ": Server already running!");
}
}
public void wait_for_file() {
try {
socket = server.accept();
byte[] buffer = new byte[maxsize];
InputStream is = socket.getInputStream();
file_name = fileTF.getText();
String[] splitfile = file_name.split("\\.");
File file = new File(splitfile[0] + "_" + getRandom() + "." + splitfile[1]);
//File file = new File(file_name);
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream out = new BufferedOutputStream(fos);
byteread = is.read(buffer, 0, buffer.length);
current = byteread;
do {
byteread = is.read(buffer, 0, buffer.length - current);
System.out.println(byteread);
if(byteread >= 0) current += byteread;
} while (byteread > -1);
out.write(buffer, 0, current);
out.flush();
fos.close();
is.close();
server.close();
}catch(Exception ex) {ex.printStackTrace();}
server_up = false;
start_server();
}
和app(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 "";
}
}
OpenFileActivity.java
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();
}
}
是的,我的代码需要很多改进,稍后会有。
有谁知道如何让它发送更大的文件?那太好了。我也在运行Windows 7 java 8 64位。和android 7.0