• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sending image to server using HTTP connection

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using the Nokia Series 60 emulator and MMAPI to capture and image and send it to a server via HTTP.
Can this be done using HTTP POST or is there another way to get the image to a server via HTTP? If someone has done this before, could you send some sample code of what the HTTP connection properties look like and how the image bytes are sent through the output stream?
On the server side I am trying to use PHP to display the png image sent from the phone on a web page.
Any help is greatly appreciated!
 
author
Posts: 1436
6
Python TypeScript Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you can. No special connection property is needed. I just write the entire byte array to the stream and then readFully() from the servlet. The server then saves it a local file with an appropriate extension (according to you capturing options). But I do not know to do it with PHP servers. If you know, please post back.
On the client side:

On the server side

[ March 21, 2004: Message edited by: Michael Yuan ]
 
Greg Schwartz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply Michael. We originally tried using the code in your reply but this didn't seem to work with PHP. We just found that the following solution works when receiving the image with a PHP page.
client side:

PHP server side:


In the above example "bytes" is the name of the POST variable and the content-type must be "multipart/form-data". The "boundary" string is also needed for "multipart/form-data" transfers. The PHP code creates a file from the "bytes" POST variable called 'testDATE.png' where DATE is the time the file was created.
Regards,
Greg
 
Michael Yuan
author
Posts: 1436
6
Python TypeScript Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great. Thanks for the tip! When I think about it, you can use Base64 to encode byte arrays to ASCII strings on both ends of the communication as well.
cheers
Michael
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The third post given shows example that it is correct i tried the same example...but its not sending anything to server.
 
leena bhegde
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The third post given shows example that it is correct i tried the same example...but its not sending anything to server.
Please reply as soon as possible
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This will help u..............

PHP code

<?php

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name'])." has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}

?>

Java code

import java.io.OutputStream;
import java.io.InputStream;
import java.net.URLConnection;
import java.net.URL;
import java.net.Socket;

public class Main {

private final String CrLf = "\r\n";

public static void main(String[] args) {
Main main = new Main();
main.httpConn();
}


private void httpConn(){

URLConnection conn = null;
OutputStream os = null;
InputStream is = null;

try{
URL url = new URL("http://localhost/test/post.php");
System.out.println("url:" + url);
conn = url.openConnection();
conn.setDoOutput(true);

String postData = "";

InputStream imgIs = getClass().getResourceAsStream("/test.jpg");
byte []imgData = new byte[imgIs.available()];
imgIs.read(imgData);

String message1 = "";
message1 += "-----------------------------4664151417711" + CrLf;
message1 += "Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"test.jpg\"" + CrLf;
message1 += "Content-Type: image/jpeg" + CrLf;
message1 += CrLf;

// the image is sent between the messages in the multipart message.

String message2 = "";
message2 += CrLf + "-----------------------------4664151417711--" + CrLf;

conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=---------------------------4664151417711");
// might not need to specify the content-length when sending chunked data.
conn.setRequestProperty("Content-Length", String.valueOf((message1.length() + message2.length() + imgData.length)));

System.out.println("open os");
os = conn.getOutputStream();

System.out.println(message1);
os.write(message1.getBytes());

// SEND THE IMAGE
int index = 0;
int size = 1024;
do{
System.out.println("write:" + index);
if((index+size)>imgData.length){
size = imgData.length - index;
}
os.write(imgData, index, size);
index+=size;
}while(index<imgData.length);
System.out.println("written:" + index);

System.out.println(message2);
os.write(message2.getBytes());
os.flush();

System.out.println("open is");
is = conn.getInputStream();

char buff = 512;
int len;
byte []data = new byte[buff];
do{
System.out.println("READ");
len = is.read(data);

if(len > 0){
System.out.println(new String(data, 0, len));
}
}while(len>0);

System.out.println("DONE");

}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("Close connection");
try{
os.close();
}catch(Exception e){}
try{
is.close();
}catch(Exception e){}
try{

}catch(Exception e){}
}
}
}


Change the URL, image path and image name and try it .................

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Manuja Jayamanne - you just failed to mention you got this code from Sony Ericsson website
 
Manuja Jayamanne
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Little bit delayed for reply.
Actually sorry for not mentioning the site. Anyway this code will help to lot of people
 
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Manuja I had check your code I'm getting an error in line 46 and 25 can you help null pointer exception
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic