Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 如何将图像数据从j2me发布到ASP.NETMVC3?_Asp.net Mvc 3_Post_Java Me_Bytearray - Fatal编程技术网

Asp.net mvc 3 如何将图像数据从j2me发布到ASP.NETMVC3?

Asp.net mvc 3 如何将图像数据从j2me发布到ASP.NETMVC3?,asp.net-mvc-3,post,java-me,bytearray,Asp.net Mvc 3,Post,Java Me,Bytearray,我正在制作一个j2me移动应用程序,它可以在ASP.NETMVC3网站上发布图像(作为字节[])和其他简单数据(字符串、整数、浮点等)。目前,除了应用程序可以在网站上发布图像数据的部分外,应用程序和网站几乎都完成了 以下是我想发布到网站(j2me)的数据模型: 这是我的网站所期望的模式(ASP.net MVC3 C#): 这是我用来发送数据的(简化)代码(j2me): 我在网上找到了一些解决方案,用于处理来自j2me应用程序的post请求,该应用程序并不是我想要的,而是在VB中。但可能其中有一些

我正在制作一个j2me移动应用程序,它可以在ASP.NETMVC3网站上发布图像(作为字节[])和其他简单数据(字符串、整数、浮点等)。目前,除了应用程序可以在网站上发布图像数据的部分外,应用程序和网站几乎都完成了

以下是我想发布到网站(j2me)的数据模型:

这是我的网站所期望的模式(ASP.net MVC3 C#):

这是我用来发送数据的(简化)代码(j2me):

我在网上找到了一些解决方案,用于处理来自j2me应用程序的post请求,该应用程序并不是我想要的,而是在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()
我不知道如何在我的网站上接收图像数据。我需要在控制器操作中输入什么代码才能接收所有数据?我需要更改我的应用程序代码中的任何内容吗

我把简单的数据从图像数据中分离出来。这是在这里工作的正确方式吗


非常感谢

我不是j2me专家,但您可以简单地发出请求,如中所示,除了HTTP请求中的简单值之外,还可以发送文件。因此,您的代码将遵循以下几行:

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();
然后,在ASP.NET MVC端,您的视图模型将如下所示:

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)
{
    ...
}

以下是
HttpMultipartRequest
代码(以防诺基亚网站宕机):


我目前面临其他问题,无法运行J2ME应用程序,因此无法测试您的解决方案。只要我能做到,我会让你知道你的解决方案是否有效。
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)
{
    ...
}
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;
    }
}