My code for uploading the image to the server:
String userIdParameter = String.valueOf(userId);
String fileName = "temporary_holder.jpg";
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
String sourceFileUri = HomeScreen.get_path();
String upLoadServerUri = "http://10.120.10.87:8080/WebImage/UploadImage";
File sourceFile = new File(sourceFileUri);
if (!sourceFile.isFile()) {
Log.e("Huzza", "Source File Does not exist");
return;
}
int serverResponseCode = 0;
try {
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("file_name", fileName);
conn.setRequestProperty("file_name_audio", fileName);
conn.setRequestProperty("X-myapp-param1", userIdParameter);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"file_name\";filename=\""
+ fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
int streamSize = (int) sourceFile.length();
bufferSize = streamSize / 10;
System.out.println("streamSize" + streamSize);
buffer = new byte[streamSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
int count = 0;
while (bytesRead > 0) {
progress = (int) (count);
displayNotification();
Thread.sleep(500);
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
count += 10;
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
System.out.println("Upload file to serverHTTP Response is : "
+ serverResponseMessage + ": " + serverResponseCode);
System.out.println("Upload file to server" + fileName
+ " File is written");
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
ex.printStackTrace();
Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (Exception e) {
e.printStackTrace();
}
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println("RESULT Message: " + line);
}
rd.close();
} catch (IOException ioex) {
Log.e("Huzza", "error: " + ioex.getMessage(), ioex);
}
return;
Uploading the image to the server works fine. I need to upload both an mp3 file and an image to the server. Please, help
source
share