Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 28th, 2012  |  syntax: None  |  size: 5.66 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to post image data from j2me to ASP.net MVC 3?
  2. public class DataModel {
  3.     private String description = null;
  4.     private float latitude = 0;
  5.     private float longitude = 0;
  6.     private long timestamp = 0;
  7.     private String userName = null;
  8.     private byte[] imageData = null;
  9.     private String contentType = null;
  10.  
  11.     // getters and setters...
  12. }
  13.        
  14. public class Model
  15. {
  16.     public string Description { get; set; }
  17.     public float Latitude { get; set; }
  18.     public float Longitude { get; set; }
  19.     public long Timestamp { get; set; }
  20.     public string UserName { get; set; }
  21.     public byte[] Image { get; set; }
  22. }
  23.        
  24. InputStream in = null;
  25. OutputStream out = null;
  26.  
  27. // url contains all the simple data
  28. String encodedUrl = UrlEncoder.encodeUrl(url);
  29. this.connection = (HttpConnection)Connector.open(encodedUrl);
  30. byte[] imageData = DataModel.getImageData();
  31.  
  32. this.connection.setRequestMethod(HttpConnection.POST);
  33. this.connection.setRequestProperty("Content-Length", imageData.length + "");
  34.  
  35. out = this.connection.openOutputStream();
  36. out.write(imageData);
  37.  
  38. int responseCode = this.connection.getResponseCode();
  39.  
  40. if(responseCode != HttpConnection.HTTP_OK) {
  41.     throw new IOException("Transmission failed as server responded with response code: " + responseCode);
  42. }
  43. // process response here...
  44.        
  45. ' the stream will be ASCII encoded'
  46. Dim ascii As ASCIIEncoding = New ASCIIEncoding
  47.  
  48. 'Get ASCII into reg. string here'
  49. strmContent = ascii.GetString(strArr)
  50. Label1.Text = strArr.ToString()
  51.  
  52. 'write the received data to a text file'
  53. Dim FILE_NAME As String = "C:\NP\received.txt"
  54. Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
  55. objWriter.WriteLine(strmContent)
  56. objWriter.WriteLine()
  57. objWriter.Close()
  58.        
  59. byte[] fileBytes = DataModel.getImageData();
  60. Hashtable params = new Hashtable();
  61. params.put("Description", "some description");
  62. params.put("Latitude", "5");
  63. params.put("Longitude", "6");
  64. params.put("Timestamp", "123");
  65. params.put("UserName", "john smith");
  66. HttpMultipartRequest req = new HttpMultipartRequest(
  67.     "http://example.com/home/upload",
  68.     params,
  69.     "Image", "original_filename.png", "image/png", fileBytes
  70. );
  71. byte[] response = req.send();
  72.        
  73. public class MyViewModel
  74. {
  75.     public string Description { get; set; }
  76.     public float Latitude { get; set; }
  77.     public float Longitude { get; set; }
  78.     public long Timestamp { get; set; }
  79.     public string UserName { get; set; }
  80.     public HttpPostedFileBase Image { get; set; }
  81. }
  82.        
  83. [HttpPost]
  84. public ActionResult Upload(MyViewModel model)
  85. {
  86.     ...
  87. }
  88.        
  89. import java.io.ByteArrayOutputStream;
  90. import java.io.InputStream;
  91. import java.io.OutputStream;
  92. import java.util.Enumeration;
  93. import java.util.Hashtable;
  94.  
  95. import javax.microedition.io.Connector;
  96. import javax.microedition.io.HttpConnection;
  97.  
  98. public class HttpMultipartRequest
  99. {
  100.     static final String BOUNDARY = "----------V2ymHFg03ehbqgZCaKO6jy";
  101.  
  102.     byte[] postBytes = null;
  103.     String url = null;
  104.  
  105.     public HttpMultipartRequest(String url, Hashtable params, String fileField, String fileName, String fileType, byte[] fileBytes) throws Exception
  106.     {
  107.         this.url = url;
  108.  
  109.         String boundary = getBoundaryString();
  110.  
  111.         String boundaryMessage = getBoundaryMessage(boundary, params, fileField, fileName, fileType);
  112.  
  113.         String endBoundary = "rn--" + boundary + "--rn";
  114.  
  115.         ByteArrayOutputStream bos = new ByteArrayOutputStream();
  116.  
  117.         bos.write(boundaryMessage.getBytes());
  118.  
  119.         bos.write(fileBytes);
  120.  
  121.         bos.write(endBoundary.getBytes());
  122.  
  123.         this.postBytes = bos.toByteArray();
  124.  
  125.         bos.close();
  126.     }
  127.  
  128.     String getBoundaryString()
  129.     {
  130.         return BOUNDARY;
  131.     }
  132.  
  133.     String getBoundaryMessage(String boundary, Hashtable params, String fileField, String fileName, String fileType)
  134.     {
  135.         StringBuffer res = new StringBuffer("--").append(boundary).append("rn");
  136.  
  137.         Enumeration keys = params.keys();
  138.  
  139.         while(keys.hasMoreElements())
  140.         {
  141.             String key = (String)keys.nextElement();
  142.             String value = (String)params.get(key);
  143.  
  144.             res.append("Content-Disposition: form-data; name="").append(key).append(""rn")    
  145.                 .append("rn").append(value).append("rn")
  146.                 .append("--").append(boundary).append("rn");
  147.         }
  148.         res.append("Content-Disposition: form-data; name="").append(fileField).append(""; filename="").append(fileName).append(""rn")
  149.             .append("Content-Type: ").append(fileType).append("rnrn");
  150.  
  151.         return res.toString();
  152.     }
  153.  
  154.     public byte[] send() throws Exception
  155.     {
  156.         HttpConnection hc = null;
  157.  
  158.         InputStream is = null;
  159.  
  160.         ByteArrayOutputStream bos = new ByteArrayOutputStream();
  161.  
  162.         byte[] res = null;
  163.  
  164.         try
  165.         {
  166.             hc = (HttpConnection) Connector.open(url);
  167.  
  168.             hc.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + getBoundaryString());
  169.  
  170.             hc.setRequestMethod(HttpConnection.POST);
  171.  
  172.             OutputStream dout = hc.openOutputStream();
  173.  
  174.             dout.write(postBytes);
  175.  
  176.             dout.close();
  177.  
  178.             int ch;
  179.  
  180.             is = hc.openInputStream();
  181.  
  182.             while ((ch = is.read()) != -1)
  183.             {
  184.                 bos.write(ch);
  185.             }
  186.             res = bos.toByteArray();
  187.         }
  188.         catch(Exception e)
  189.         {
  190.             e.printStackTrace();
  191.         }
  192.         finally
  193.         {
  194.             try
  195.             {
  196.                 if(bos != null)
  197.                     bos.close();
  198.  
  199.                 if(is != null)
  200.                     is.close();
  201.  
  202.                 if(hc != null)
  203.                     hc.close();
  204.             }
  205.             catch(Exception e2)
  206.             {
  207.                 e2.printStackTrace();
  208.             }
  209.         }
  210.         return res;
  211.     }
  212. }