我想将图像上传到我的本地apache tomscat服务器,并且正在寻找示例代码。 请让我知道如果服务器允许或不reciving文件,如果有任何其他的方法从Android将图像发送到Apache Tomcat服务器使用android将图像上传到apache tomcat服务器
0
A
回答
3
检查;)
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.util.Log;
public class HttpFileUploader {
URL connectURL;
String params;
String responseString;
String fileName;
byte[] dataToServer;
FileInputStream fileInputStream = null;
HttpFileUploader(String urlString, String params, String fileName) {
try {
connectURL = new URL(urlString);
} catch (Exception ex) {
Log.i("URL FORMATION", "MALFORMATED URL");
}
this.params = params + "=";
this.fileName = fileName;
}
void doStart(FileInputStream stream) {
fileInputStream = stream;
String exsistingFileName = "asdf.png";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
String Tag = "3rd";
try {
// ------------------ CLIENT REQUEST
Log.e(Tag, "Starting to bad things");
// Open a HTTP connection to the URL
HttpURLConnection conn = (HttpURLConnection) connectURL
.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos
.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
+ exsistingFileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
Log.e(Tag, "Headers are written");
// create a buffer of maximum size
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
// read file and write it into form...
int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
Log.e(Tag, "File is written");
fileInputStream.close();
dos.flush();
InputStream is = conn.getInputStream();
// retrieve the response from server
int ch;
StringBuffer b = new StringBuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
String s = b.toString();
Log.i("Response", s);
dos.close();
} catch (MalformedURLException ex) {
Log.e(Tag, "error: " + ex.getMessage(), ex);
}
catch (IOException ioe) {
Log.e(Tag, "error: " + ioe.getMessage(), ioe);
}
}
}
+0
谢谢你生病尝试使用这 –
+0
这段代码在我的应用程序中工作,所以在你的应用程序,应该也工作;)和不要忘记投票,当你将有超过15分; D –
相关问题
- 1. android将图像上传到服务器
- 2. 上传图像到Apache服务器在群集的tomcat
- 3. Android:使用Apache服务器将图像上传到Web Serbice的问题
- 4. 无法使用Android Volley将图像上传到服务器
- 5. 使用PaperClip将图像从Android上传到Rails服务器
- 6. 使用Base64将图像从android上传到服务器
- 7. 将图像上传到服务器symfony2
- 8. 将图像上传到服务器ImageSwitcher
- 9. 将图像上传到从服务器
- 10. 将图像上传到服务器angularjs
- 11. 将图像上传到服务器
- 12. 在apache tomcat服务器上使用CakePHP
- 13. [Android] - 将图片上传到服务器
- 14. 从android应用程序将图像上传到服务器
- 15. 如何在Android上将图像保存/上传到服务器?
- 16. 如何使用Apache Jmeter将许多图像上传到服务器?
- 17. 上传图像服务器上传图像服务器
- 18. 从android phonegap上传图像到服务器使用asmx
- 19. 上传图像到服务器使用multipartentity和json在android
- 20. Android MySql使用php创建并上传图像到服务器
- 21. 将图像上传到服务器上之前旋转图像
- 22. 如何使用android排球库将位图图像上传到服务器?
- 23. 访问图像,我已经上传到tomcat服务器
- 24. 使用ColdFusion将上传的图像保存到服务器
- 25. 使用SOAP webservice将图像上传到服务器
- 26. 使用Mongoose将多个图像上传到Node.js服务器
- 27. 使用iPhone将图像上传到服务器
- 28. 使用Alamofire将图像上传到API服务器
- 29. 使用博客将图像上传到服务器PHP
- 30. 是否可以使用Lightroom将图像上传到服务器?
这可能会有所帮助:HTTP:/ /stackoverflow.com/questions/3288502/upload-file-on-apache-server –