자바 애플 리케이션을 실행하는 서버에 이미지를 보내는 안드로이드 애플 리케이션을 작성 중이며 아주 이상한 방식으로 작동하고 있습니다! 여기 이상한 행동 : 안드로이드 폰에서 자바 서버로 이미지 보내기 (코드 작업)
내가- 컴파일을하고
- 컴파일이 배포에 이미지를 전송하는 안드로이드 일부를 실행하는 서버의 역할을 일부를받을 수와 바탕 화면에 자바 응용 프로그램을 실행하는 것입니다 서버. 안드로이드 앱 실행이 끝났지 만 자바 앱이 실행되지 않는다.
- 이미지를 다시 보내려면 코드가있는 안드로이드 앱 활동을 실행하고, 이번에는 안드로이드 앱 진행 대화 상자가 멈추지 만 자바 앱 실행이 끝나고 안드로이드 응용 프로그램에 SEND 부분에 대한 코드
class ProjectServer { ServerSocket serSock; Socket sock; BufferedReader in; PrintWriter out; public static void main(String ar[]) { try { ProjectServer cs=new ProjectServer(); cs.startServer(); } catch(Exception e) { } } public void startServer() { try { serSock=new ServerSocket(8070); System.out.println("Waiting for client..."); sock=serSock.accept(); System.out.println("Connections done"); //Accept File System.out.println("Connected"); //receive code int filesize=450660; int bytesRead; int current=0; // receive file byte [] mybytearray = new byte [filesize]; InputStream is = sock.getInputStream(); FileOutputStream fos = new FileOutputStream("C:\\Project Server\\Capture.png"); BufferedOutputStream bos = new BufferedOutputStream(fos); bytesRead = is.read(mybytearray,0,mybytearray.length); current = bytesRead; do { bytesRead = is.read(mybytearray, current, (mybytearray.length-current)); if(bytesRead >= 0) current += bytesRead; } while(bytesRead > -1); bos.write(mybytearray, 0 , current); bos.flush(); System.out.println("end-start"); } catch(Exception e) { System.out.println(e); e.printStackTrace(); } } }
다음입니다 : : 또한 이미지 전송을 성공적으로 디 ...
이 다음은 자바 응용 프로그램의 수신부에 대한 코드입니다
package com.site.custom;
public class Act2 extends Activity
{
private ProgressDialog pd;
private String serverIP="58.146.100.187";
private BufferedReader in;
private PrintWriter out;
private String path;
private Socket cliSock;
public void onCreate(Bundle onCreateInstance)
{
super.onCreate(onCreateInstance);
setContentView(R.layout.act2);
this.setTitle("This has started");
path=getIntent().getStringExtra("path");
//Establish Connection
try
{
cliSock=new Socket(serverIP,8070);
in=new BufferedReader(new InputStreamReader(cliSock.getInputStream()));
((TextView)findViewById(R.id.tview)).setText(path);
}
catch(Exception e)
{
Log.v("MERA MSG",e.toString());
}
//Send file
ProgressDialog pd=ProgressDialog.show(this, "Sending image", "Image chosen:"+path.substring(path.lastIndexOf("//")+1),false,true);
try
{
File myFile = new File (path);
System.out.println((int)myFile.length());
byte[] mybytearray = new byte[450560];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = cliSock.getOutputStream();
System.out.println("Sending...");
os.write(mybytearray,0,mybytearray.length);
os.flush();
System.out.println("Completed");
pd.dismiss();
System.out.println("Done");
}
catch(Exception e)
{
Log.v("MERA MSG",e.toString());
}
}
}
Holyyy! @WATTO Studios 당신을 사랑합니다 !!!!! – GrowinMan
당신이 옳은데, 출력 스트림을 닫지 않고 강제로 플러시 할 수있는 방법이 있습니까? 스트림을 닫으면 소켓이 닫혀서 여기에 언급 된 문제가 발생하기 때문에 http://stackoverflow.com/questions/10163358/socketexception-socket-is-closed-even-when-isconnected-returns- 사실 # comment13038342_10163358 – GrowinMan
위의 답변에 몇 가지 코드를 추가 했으므로 close()가 호출 되더라도 열린 채로있는'Socket'을 만들 수 있습니다. 희망이 도움이됩니다. – wattostudios