Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.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
Java 在web服务器上发布图像问题_Java_Php_Blackberry_Post_Upload - Fatal编程技术网

Java 在web服务器上发布图像问题

Java 在web服务器上发布图像问题,java,php,blackberry,post,upload,Java,Php,Blackberry,Post,Upload,我正在尝试将jpg图像发布到web服务器。我已经在服务器上测试了我的PHP脚本,我能够使用表单上传一个图像。现在,我正在尝试使用相同的脚本制作一个Blackberry应用程序,将图像发布到服务器上。但是,当我测试Java代码时,PHP告诉我没有发布任何内容,我不确定我做错了什么,但我正在做类似的事情: String mBoundary = "SUPSUPSUPSUP"; /* Preparar objeto a enviar */ InputStream mImagen = this.get

我正在尝试将jpg图像发布到web服务器。我已经在服务器上测试了我的PHP脚本,我能够使用表单上传一个图像。现在,我正在尝试使用相同的脚本制作一个Blackberry应用程序,将图像发布到服务器上。但是,当我测试Java代码时,PHP告诉我没有发布任何内容,我不确定我做错了什么,但我正在做类似的事情:

String mBoundary = "SUPSUPSUPSUP";

/* Preparar objeto a enviar */

InputStream mImagen = this.getClass().getResourceAsStream("sample.jpg");            
byte[] mBytesPostear = IOUtilities.streamToBytes(mImagen);

HttpConnection mConexion = (HttpConnection) Connector.open("http://www.raspberry.com/script.php");                      

/* Preparar headers del POST. Es multiformulario con POST de un archivo */
mConexion.setRequestMethod(HttpConnection.POST);
mConexion.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_DATA + ";boundary=" + mBoundary);
mConexion.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, String.valueOf(mBytesPostear.length));
mConexion.setRequestProperty( "x-rim-transcode-content", "none" );                      

/* Preparar contenido de salida */
ByteArrayOutputStream mOutput = new ByteArrayOutputStream();            
OutputStream mOS = mConexion.openOutputStream();

/* Escribir contenido */
String nuevaLinea = "\r\n";
String contDisp="Content-Disposition:form-data; name=\"foto\";filename=\"sample.jpg\"";
String contEnc = "Content-Transfer-Encoding: binary";
String type="Content-Type:image/jpeg";

mOutput.write(nuevaLinea.getBytes());
mOutput.write("--".getBytes());
mOutput.write(mBoundary.getBytes());
mOutput.write(nuevaLinea.getBytes());
mOutput.write(contDisp.getBytes());
mOutput.write(nuevaLinea.getBytes());
mOutput.write(type.getBytes());
mOutput.write(nuevaLinea.getBytes());
mOutput.write(contEnc.getBytes());
mOutput.write(nuevaLinea.getBytes());
mOutput.write(nuevaLinea.getBytes());
mOutput.write(mBytesPostear);
mOutput.write(nuevaLinea.getBytes());
mOutput.write("--".getBytes());
mOutput.write(mBoundary.getBytes());
mOutput.write("--".getBytes());         
mOutput.write(nuevaLinea.getBytes());

/**********************/

/* Escribir el contenido */
mOS.write(mOutput.toByteArray());

mOutput.flush();
mOutput.close();

mOS.flush();
mOS.close();

/* Recibir respuesta del servidor */
InputStream mIS = mConexion.openInputStream();
int mLongitud = (int) mConexion.getLength();

if (mLongitud > 0) {                

    int mActual = 0;
    int mBytesLeidos = 0;
    byte[] mBytes = new byte[mLongitud];

    while ((mBytesLeidos != mLongitud) && (mActual != -1)){
        mActual = mIS.read(mBytes, mBytesLeidos, mLongitud - mBytesLeidos);
        mBytesLeidos += mActual;
    }               

    String mRespuesta = new String(mBytes);     
    System.out.println("Respuesta: " + mRespuesta);

} 
我只是在使用表单时尝试克隆Chrome发送的标题,我认为它们具有相同的信息

我的PHP脚本首先检查是否发布了一些内容,如果没有发布任何内容,那么它将返回一条消息,以便我能够在web服务器中“使用”该脚本,我可以看到Blackberry设备正在上传数据,但答案是没有发布任何内容

我想我正在以错误的格式将信息发送到服务器

任何帮助都将不胜感激


谢谢

我不确定是否还有其他问题,但这看起来不太正确:

mConexion.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, String.valueOf(mBytesPostear.length));

Content-Length
头应该给出完整请求正文的长度(以字节为单位),包括所有
--boundary\r\n头:stuff
,不仅仅是上传的文件字段。

我在代码中添加了完整的长度,并检查了我的PHP脚本,现在它工作正常:这是完整的代码片段,以防其他人需要它:

public class PostImagen {
private String mURL;
private byte[] mDatos;

public PostImagen(String URLServicio, byte[] Datos){
    mDatos = Datos;
    mURL = URLServicio;
}

public void getRespuesta() throws Exception {       
    try {                                      
        String mBoundary = "SUPSUP";            

        /* Strings a usar para el contenido */
        String nuevaLinea = "\r\n";
        String contDisp="Content-Disposition: multipart/form-data; name=\"foto\";filename=\"sample.jpg\"";
        String contEnc = "Content-Transfer-Encoding: binary";
        String type="Content-Type:image/jpeg";

        /* Preparar objeto a enviar */
        byte[] mBytesPostear;
        if (mDatos == null){
            InputStream mImagen = this.getClass().getResourceAsStream("sample.jpg");            
            mBytesPostear = IOUtilities.streamToBytes(mImagen);
        } else {
            mBytesPostear = mDatos;
        }                       

        System.err.println("Longitud de imagen: " + mBytesPostear.length);



        /* Preparar contenido de salida */
        ByteArrayOutputStream mOutput = new ByteArrayOutputStream();
        mOutput.write(nuevaLinea.getBytes());
        mOutput.write("--".getBytes());
        mOutput.write(mBoundary.getBytes());
        mOutput.write(nuevaLinea.getBytes());
        mOutput.write(contDisp.getBytes());
        mOutput.write(nuevaLinea.getBytes());
        mOutput.write(type.getBytes());
        mOutput.write(nuevaLinea.getBytes());
        mOutput.write(contEnc.getBytes());
        mOutput.write(nuevaLinea.getBytes());
        mOutput.write(nuevaLinea.getBytes());
        mOutput.write(mBytesPostear);
        mOutput.write(nuevaLinea.getBytes());
        mOutput.write("--".getBytes());
        mOutput.write(mBoundary.getBytes());
        mOutput.write("--".getBytes());         
        mOutput.write(nuevaLinea.getBytes());

        /* Preparar conexión y headers */
        HttpConnection mConexion = (HttpConnection) Connector.open(mURL);
        mConexion.setRequestMethod(HttpConnection.POST);
        mConexion.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_DATA + ";boundary=" + mBoundary);
        mConexion.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, String.valueOf(mOutput.size()));          
        mConexion.setRequestProperty( "x-rim-transcode-content", "none" );      

        /**********************/
        System.err.println("Escribiendo stream");

        OutputStream mOS = mConexion.openOutputStream();            

        /* Escribir el contenido */
        mOS.write(mOutput.toByteArray());

        mOutput.flush();
        mOutput.close();

        mOS.flush();
        mOS.close();

        System.err.println("Se terminó de escribir payload, recibiendo respuesta");

        /* Recibir respuesta del servidor */
        if (mConexion.getResponseCode() != HttpConnection.HTTP_OK){             
            throw new Exception("El servidor NO regresó OK (200) al leer la respuesta. Saliendo...");
        }

        InputStream mIS = mConexion.openInputStream();
        int mLongitud = (int) mConexion.getLength();

        if (mLongitud > 0) {                

            int mActual = 0;
            int mBytesLeidos = 0;
            byte[] mBytes = new byte[mLongitud];

            while ((mBytesLeidos != mLongitud) && (mActual != -1)){
                mActual = mIS.read(mBytes, mBytesLeidos, mLongitud - mBytesLeidos);
                mBytesLeidos += mActual;
            }               

            String mRespuesta = new String(mBytes);     
            System.out.println("Respuesta: " + mRespuesta);

        } else {                                            
            throw new Exception("No se recibió respuesta del servidor");
        }                                           
    } catch (IOException e) {                                   
        throw new Exception("Error de lectura o escritura: " + e.getMessage());
    }       
}

}

如果PHP代码正常工作,这可能与PHP无关。不管怎样,这可能与Java代码有关。我几乎可以肯定问题出在Java代码上,我实际上是在寻求Java代码方面的帮助。我尝试了这个方法,但似乎仍然不起作用。我检查了web服务器访问日志,当我发出请求时,我会返回一个200(OK)。我正在尝试检查错误日志,因为它是在GoDaddy托管的,我不知道怎么做,但我正在询问他们。这是我用来计算整个有效负载大小的行:int mTamañottal=mBytesPostear.length+numevalinea.length()*8+“--”.length()*3+mBoundary.length()*2+contDisp.length()+type.length()+contEnc.length();