服务器发送的图片怎么显示在程序,如何从Android发送图像数据到服务器

您只需要在POST的特殊情况下执行Http-FileUpload.

无需对文件进行uuencode.

无需使用特殊的lib / jar

无需将对象保存到磁盘(无论以下示例是这样做的)

您可以找到关于Http-Command的非常好的解释,并作为您特别关注的“文件上传”

从那里的文件上传样本如下(观看“发送二进制文件”)

并且可以添加一些伴随数据

String param = "value";

File textFile = new File("/path/to/file.txt");

File binaryFile = new File("/path/to/file.bin");

String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.

String CRLF = "\r\n"; // Line separator required by multipart/form-data.

URLConnection connection = new URL(url).openConnection();

connection.setDoOutput(true);

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

PrintWriter writer = null;

try {

OutputStream output = connection.getOutputStream();

writer = new PrintWriter(new OutputStreamWriter(output, charset), true); // true = autoFlush, important!

// Send normal param.

writer.append("--" + boundary).append(CRLF);

writer.append("Content-Disposition: form-data; name=\"param\"").append(CRLF);

writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);

writer.append(CRLF);

writer.append(param).append(CRLF).flush();

// Send text file.

writer.append("--" + boundary).append(CRLF);

writer.append("Content-Disposition: form-data; name=\"textFile\"; filename=\"" + textFile.getName() + "\"").append(CRLF);

writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);

writer.append(CRLF).flush();

BufferedReader reader = null;

try {

reader = new BufferedReader(new InputStreamReader(new FileInputStream(textFile), charset));

for (String line; (line = reader.readLine()) != null;) {

writer.append(line).append(CRLF);

}

} finally {

if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}

}

writer.flush();

// Send binary file.

writer.append("--" + boundary).append(CRLF);

writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);

writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName()).append(CRLF);

writer.append("Content-Transfer-Encoding: binary").append(CRLF);

writer.append(CRLF).flush();

InputStream input = null;

try {

input = new FileInputStream(binaryFile);

byte[] buffer = new byte[1024];

for (int length = 0; (length = input.read(buffer)) > 0;) {

output.write(buffer, 0, length);

}

output.flush(); // Important! Output cannot be closed. Close of writer will close output as well.

} finally {

if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}

}

writer.append(CRLF).flush(); // CRLF is important! It indicates end of binary boundary.

// End of multipart/form-data.

writer.append("--" + boundary + "--").append(CRLF);

} finally {

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

}

关于你问题的第二部分.成功上传文件(我使用apache常用文件)后,将blob作为图像传递并不是什么大问题.

这是如何接受servlet中的文件

public void doPost(HttpServletRequest pRequest, HttpServletResponse pResponse)

throws ServletException, IOException {

ServletFileUpload upload = new ServletFileUpload();

try {

FileItemIterator iter = upload.getItemIterator (pRequest);

while (iter.hasNext()) {

FileItemStream item = iter.next();

String fieldName = item.getFieldName();

InputStream stream = item.openStream();

....

stream.close();

}

...

这段代码提供了一个图像

public void doGet (HttpServletRequest pRequest, HttpServletResponse pResponse) throws IOException {

try {

Blob img = (Blob) entity.getProperty(propImg);

pResponse.addHeader ("Content-Disposition", "attachment; filename=abc.png");

pResponse.addHeader ("Cache-Control", "max-age=120");

String enc = "image/png";

pResponse.setContentType (enc);

pResponse.setContentLength (img.getBytes().length);

OutputStream out = pResponse.getOutputStream ();

out.write (img.getBytes());

out.close();

我希望这段代码片段有助于回答您的问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值