如何在服务器上显示或使用从 Android 手机接收的图像

发布于 2024-12-12 08:32:35 字数 1926 浏览 0 评论 0原文

我正在从 Android 手机发送图像到处理它的服务器,但现在我对如何在服务器中使用图像感到困惑我

发送图像的 Android 手机代码是

                            Log.i("sAMPLE","Info:" );
                //String postURL = HOST_SERVER_URL + HOST_PHOTO_UPLOAD_URI;
                String postURL ="http://10.0.2.2:8080/SimpleServlet/simple-servlet";//server URL
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost postRequest = new HttpPost(postURL);

                ByteArrayBody bab = new ByteArrayBody(imageBytes, "file_name_ignored");
                MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                reqEntity.addPart("source", bab);
                postRequest.setEntity(reqEntity);

                HttpResponse response = httpClient.execute(postRequest); 

,处理服务器中图像的代码是像这样

        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
    boolean isMultipart = ServletFileUpload.isMultipartContent(req);
    System.out.println("Before Mutlipart");
    if(!isMultipart)
        throw new ServletException("upload using multipart");

    ServletFileUpload upload = new ServletFileUpload(fif);
    upload.setSizeMax(1024 * 1024 * 10 /* 10 mb */);
    List<FileItem> items;
    try {
        items = upload.parseRequest(req);
    //}// catch (FileUploadException e) {
      //  throw new ServletException(e);
    } catch (FileUploadException e) {
        // TODO Auto-generated catch block
        throw new ServletException(e);
    }

    if(items == null || items.size() == 0)
        throw new ServletException("No items uploaded");

    FileItem item = items.get(0);
    //BufferedImage Img=item.getString();
    System.out.println(item.getContentType());
    byte[]data=item.get();

现在我如何使用字节数组在服务器上显示图像或使用其他一些东西(如字符串、其他图像等)编辑图像。

I am sending an image from android phone to the server which hanldes it ,but now i am confused on how to use the image in the server

my code for android phone which sends the image is

                            Log.i("sAMPLE","Info:" );
                //String postURL = HOST_SERVER_URL + HOST_PHOTO_UPLOAD_URI;
                String postURL ="http://10.0.2.2:8080/SimpleServlet/simple-servlet";//server URL
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost postRequest = new HttpPost(postURL);

                ByteArrayBody bab = new ByteArrayBody(imageBytes, "file_name_ignored");
                MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                reqEntity.addPart("source", bab);
                postRequest.setEntity(reqEntity);

                HttpResponse response = httpClient.execute(postRequest); 

and my code which handles the image in the server is like this

        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
    boolean isMultipart = ServletFileUpload.isMultipartContent(req);
    System.out.println("Before Mutlipart");
    if(!isMultipart)
        throw new ServletException("upload using multipart");

    ServletFileUpload upload = new ServletFileUpload(fif);
    upload.setSizeMax(1024 * 1024 * 10 /* 10 mb */);
    List<FileItem> items;
    try {
        items = upload.parseRequest(req);
    //}// catch (FileUploadException e) {
      //  throw new ServletException(e);
    } catch (FileUploadException e) {
        // TODO Auto-generated catch block
        throw new ServletException(e);
    }

    if(items == null || items.size() == 0)
        throw new ServletException("No items uploaded");

    FileItem item = items.get(0);
    //BufferedImage Img=item.getString();
    System.out.println(item.getContentType());
    byte[]data=item.get();

now how do i use the byte array to display the image on the server or use to edit the image with some other stuffs like string,other image etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必了 2024-12-19 08:32:35

我认为这应该适合你...

http://www.mkyong.com/java/how-to-convert-byte-to-bufferedimage-in-java/

在服务器端(imageInByte 是你的数据[])...

//convert byte array back to BufferedImage
InputStream in = new ByteArrayInputStream(imageInByte);
BufferedImage bImageFromConvert = ImageIO.read(in);

ImageIO.write(bImageFromConvert, "jpg", 
         new File("c:\\image\\mypic_new.jpg")); 

I think this should work for you...

http://www.mkyong.com/java/how-to-convert-byte-to-bufferedimage-in-java/

on the server side (imageInByte is your data[])...

//convert byte array back to BufferedImage
InputStream in = new ByteArrayInputStream(imageInByte);
BufferedImage bImageFromConvert = ImageIO.read(in);

ImageIO.write(bImageFromConvert, "jpg", 
         new File("c:\\image\\mypic_new.jpg")); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文