2012-02-18 7 views
0

私はASP.net MVC 3ウェブサイトに画像(バイト[])と他の単純なデータ(文字列、ints、浮動小数点数など)を投稿できるj2meモバイルアプリケーションを作成しています。現在、アプリケーションとウェブサイトはほとんど完了していますが、アプリケーションがウェブサイトに画像データを投稿できる部分は除きます。ここでj2meからASP.netに画像データを投稿する方法MVC 3?

は、私は、ウェブサイト(J2ME)に投稿したいデータモデルである:

public class DataModel { 
    private String description = null; 
    private float latitude = 0; 
    private float longitude = 0; 
    private long timestamp = 0; 
    private String userName = null; 
    private byte[] imageData = null; 
    private String contentType = null; 

    // getters and setters... 
} 

これは私のウェブサイトは、(ASP.net MVC3 C#の)期待するモデルである:

public class Model 
{ 
    public string Description { get; set; } 
    public float Latitude { get; set; } 
    public float Longitude { get; set; } 
    public long Timestamp { get; set; } 
    public string UserName { get; set; } 
    public byte[] Image { get; set; } 
} 

私が処理するためのオンラインいくつかのsollutionsを見つけた

InputStream in = null; 
OutputStream out = null; 

// url contains all the simple data 
String encodedUrl = UrlEncoder.encodeUrl(url); 
this.connection = (HttpConnection)Connector.open(encodedUrl); 
byte[] imageData = DataModel.getImageData(); 

this.connection.setRequestMethod(HttpConnection.POST); 
this.connection.setRequestProperty("Content-Length", imageData.length + ""); 

out = this.connection.openOutputStream(); 
out.write(imageData); 

int responseCode = this.connection.getResponseCode(); 

if(responseCode != HttpConnection.HTTP_OK) { 
    throw new IOException("Transmission failed as server responded with response code: " + responseCode); 
} 
// process response here... 

:これは私がデータを送信するために使用(簡体字)コード(J2ME)でありますj2meアプリケーションからの投稿要求は、私が望むことをdoens'tし、VBにあります。しかし、おそらく、ページロードイベントに配置する必要がありますそこにいくつかの有用なコードは、あります:

' the stream will be ASCII encoded' 
Dim ascii As ASCIIEncoding = New ASCIIEncoding 

'Get ASCII into reg. string here' 
strmContent = ascii.GetString(strArr) 
Label1.Text = strArr.ToString() 

'write the received data to a text file' 
Dim FILE_NAME As String = "C:\\NP\\received.txt" 
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True) 
objWriter.WriteLine(strmContent) 
objWriter.WriteLine() 
objWriter.Close() 

私は私のウェブサイト上の画像データを受信することができるか見当もつかない。すべてのデータを受信できるようにするには、コントローラアクションにどのコードを入力する必要がありますか?私はアプリケーションコード内の何かを変更する必要がありますか?

私は画像データから単純なデータを分割しています。それはここでも働く正しい方法ですか?

ありがとう!

答えて

1

私はj2meの専門家ではありませんが、multipart/form-dataリクエストをthis articleのように作成すると、HTTPリクエストの単純な値に加えてファイルを送信できます。だから、あなたのコードは、の線に沿って何かになります:あなたのビューモデル側あなたのASP.NET MVCに続いて

byte[] fileBytes = DataModel.getImageData(); 
Hashtable params = new Hashtable(); 
params.put("Description", "some description"); 
params.put("Latitude", "5"); 
params.put("Longitude", "6"); 
params.put("Timestamp", "123"); 
params.put("UserName", "john smith"); 
HttpMultipartRequest req = new HttpMultipartRequest(
    "http://example.com/home/upload", 
    params, 
    "Image", "original_filename.png", "image/png", fileBytes 
); 
byte[] response = req.send(); 

を単に次のようになります。

public class MyViewModel 
{ 
    public string Description { get; set; } 
    public float Latitude { get; set; } 
    public float Longitude { get; set; } 
    public long Timestamp { get; set; } 
    public string UserName { get; set; } 
    public HttpPostedFileBase Image { get; set; } 
} 

とあなたのコントローラのアクション:

[HttpPost] 
public ActionResult Upload(MyViewModel model) 
{ 
    ... 
} 

そして、ここでは(場合にはNokiaのサイトがダウンした)HttpMultipartRequestコードです:

import java.io.ByteArrayOutputStream; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.Enumeration; 
import java.util.Hashtable; 

import javax.microedition.io.Connector; 
import javax.microedition.io.HttpConnection; 

public class HttpMultipartRequest 
{ 
    static final String BOUNDARY = "----------V2ymHFg03ehbqgZCaKO6jy"; 

    byte[] postBytes = null; 
    String url = null; 

    public HttpMultipartRequest(String url, Hashtable params, String fileField, String fileName, String fileType, byte[] fileBytes) throws Exception 
    { 
     this.url = url; 

     String boundary = getBoundaryString(); 

     String boundaryMessage = getBoundaryMessage(boundary, params, fileField, fileName, fileType); 

     String endBoundary = "\r\n--" + boundary + "--\r\n"; 

     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

     bos.write(boundaryMessage.getBytes()); 

     bos.write(fileBytes); 

     bos.write(endBoundary.getBytes()); 

     this.postBytes = bos.toByteArray(); 

     bos.close(); 
    } 

    String getBoundaryString() 
    { 
     return BOUNDARY; 
    } 

    String getBoundaryMessage(String boundary, Hashtable params, String fileField, String fileName, String fileType) 
    { 
     StringBuffer res = new StringBuffer("--").append(boundary).append("\r\n"); 

     Enumeration keys = params.keys(); 

     while(keys.hasMoreElements()) 
     { 
      String key = (String)keys.nextElement(); 
      String value = (String)params.get(key); 

      res.append("Content-Disposition: form-data; name=\"").append(key).append("\"\r\n")  
       .append("\r\n").append(value).append("\r\n") 
       .append("--").append(boundary).append("\r\n"); 
     } 
     res.append("Content-Disposition: form-data; name=\"").append(fileField).append("\"; filename=\"").append(fileName).append("\"\r\n") 
      .append("Content-Type: ").append(fileType).append("\r\n\r\n"); 

     return res.toString(); 
    } 

    public byte[] send() throws Exception 
    { 
     HttpConnection hc = null; 

     InputStream is = null; 

     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

     byte[] res = null; 

     try 
     { 
      hc = (HttpConnection) Connector.open(url); 

      hc.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + getBoundaryString()); 

      hc.setRequestMethod(HttpConnection.POST); 

      OutputStream dout = hc.openOutputStream(); 

      dout.write(postBytes); 

      dout.close(); 

      int ch; 

      is = hc.openInputStream(); 

      while ((ch = is.read()) != -1) 
      { 
       bos.write(ch); 
      } 
      res = bos.toByteArray(); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
     finally 
     { 
      try 
      { 
       if(bos != null) 
        bos.close(); 

       if(is != null) 
        is.close(); 

       if(hc != null) 
        hc.close(); 
      } 
      catch(Exception e2) 
      { 
       e2.printStackTrace(); 
      } 
     } 
     return res; 
    } 
} 
+0

私は現在、私のJ2MEアプリケーションの実行を妨げる他の問題に直面しているので、私はあなたの解決策をテストできません。それがうまくいくように、私はあなたの解決策が働くかどうかを知らせます。 –

関連する問題