gpt4 book ai didi

java - 在网络服务器上发布图像问题

转载 作者:行者123 更新时间:2023-12-02 08:31:35 25 4
gpt4 key购买 nike

我正在尝试将 jpg 图像发布到网络服务器。我已经在服务器上测试了我的 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 在使用表单时发送的 header ,我认为它们具有相同的信息。

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

我认为我以错误的格式向服务器发送信息。

任何帮助将不胜感激。

谢谢!

最佳答案

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

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

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

关于java - 在网络服务器上发布图像问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3221281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com