我正在制作一个j2me移动应用程序,它可以在ASP.net MVC 3网站上发布图像(如byte [])和其他简单数据(字符串,整数,浮点等)。目前应用程序和网站几乎已完成,除了应用程序可以在网站上发布图像数据的部分。如何将图像数据从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; }
}
这是(简化)的代码,我用它来发送数据(J2ME):
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...
我发现了一些sollutions在线处理一个来自一个j2me应用程序的请求,它不会做我想做的事情,它在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()
我不知道我怎么能接受我的网站上的图像数据。我需要将哪些代码放入Controller Action才能够接收所有数据?我需要更改我的应用程序代码中的任何内容吗?
我从图像数据中分离出简单的数据。这是否正确的工作方式?
非常感谢!
我目前正面临其他问题阻止我运行我的J2ME应用程序,所以我无法测试您的溶剂。就像我得到那份工作一样,我会让你知道你的溶剂是否有效。 – 2012-02-25 09:43:19