Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.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 使用socket将文件从Android上传到服务器_Java_Android_Sockets - Fatal编程技术网

Java 使用socket将文件从Android上传到服务器

Java 使用socket将文件从Android上传到服务器,java,android,sockets,Java,Android,Sockets,我正在开发Android应用程序。在创建这个问题之前,我搜索了很多帖子。我想用java中的socket从android手机上传文件。服务器端应该是什么样的应用程序?假设用java编写服务器端,它应该是什么样的项目? 关于java应用程序,我只知道服务器主机——tomcat。在您的情况下(因为服务器有tomcat),如果您有服务器的url,那么您可以使用HttpURLConnection将任何文件上载到服务器。在服务器端,应该写入逻辑来接收文件 示例 HttpURLConnection conne

我正在开发Android应用程序。在创建这个问题之前,我搜索了很多帖子。我想用java中的socket从android手机上传文件。服务器端应该是什么样的应用程序?假设用java编写服务器端,它应该是什么样的项目? 关于java应用程序,我只知道服务器主机——tomcat。

在您的情况下(因为服务器有tomcat),如果您有服务器的url,那么您可以使用HttpURLConnection将任何文件上载到服务器。在服务器端,应该写入逻辑来接收文件 示例

HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;

String pathToOurFile = "/sdcard/file_to_send.mp3"; //complete path of file from your android device
String urlServer = "http://192.168.10.1/handle_upload.do";// complete path of server
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary =  "*****";

int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;

try
{
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );

URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();

// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);

// Enable POST method
connection.setRequestMethod("POST");

connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

outputStream = new DataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
outputStream.writeBytes(lineEnd);

bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];

// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);

while (bytesRead > 0)
{
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}

outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

// Responses from the server (code and message)
serverResponseCode = connection.getResponseCode();
serverResponseMessage = connection.getResponseMessage();

fileInputStream.close();
outputStream.flush();
outputStream.close();
}
catch (Exception ex)
{
//Exception handling
}

+1篇很好的帖子,也许是一篇简短的总结,说明服务器端应该匹配什么样的代码,这对我很有用。Sunil,你的解决方案,是否意味着我需要创建一个Web服务或网站来接收文件?有没有一种方法可以在服务器端不使用代码的情况下从客户端获取文件?喜欢FTP吗?检查SPK的链接以获得FTP上传。它链接到一个预先制作的FTP类来为您进行上传。但请记住,使用标准FTP上传可能不安全(因为任何人都可能窃取登录数据并滥用服务器!)。