Java 在 JSON 对象中嵌入文件附件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22668660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:12:15  来源:igfitidea点击:

Embedding a File Attachment in JSON Object

javahtmljsonfile

提问by Chinthaka Dharmasiri

Is it possible to embed a file attachment in a JSON Object. I have a HTML Form, which has several text field inputs and a file attachment. I want to send a JSON Object wrapping all these form data (including the file attachment) to the server.

是否可以在 JSON 对象中嵌入文件附件。我有一个 HTML 表单,它有几个文本字段输入和一个文件附件。我想将包装所有这些表单数据(包括文件附件)的 JSON 对象发送到服务器。

Are there any particular libraries in Java available to do that? Can you give possible solution for this.

Java中是否有任何特定的库可以做到这一点?您能否为此提供可能的解决方案。

Thanks

谢谢

采纳答案by Starscream1984

If you want to send the actual data of the file, you'd probably want to encode it as a base64 string and send that in your JSON- see fiddle for example of encoding it in javascript:

如果您想发送文件的实际数据,您可能希望将其编码为 base64 字符串并将其发送到您的JSON- 请参阅 fiddle 例如在 javascript 中对其进行编码:

http://jsfiddle.net/eliseosoto/JHQnk/

http://jsfiddle.net/eliseosoto/JHQnk/

Then you could do the opposite on your server-side using whatever language and/or libraries are appropriate.

然后,您可以使用任何合适的语言和/或库在服务器端执行相反的操作。

回答by Mr Boss

Use MultipartEntity, someone else posted a similar question: How to send file in JSON on android?You could also consider saving the files on the server and sending a path/url to the file location where the other server can access them.

使用MultipartEntity,别人贴了一个类似的问题:How to send file in JSON on android? 您还可以考虑将文件保存在服务器上,并将路径/url 发送到其他服务器可以访问它们的文件位置。

 public String SendToServer(String aUrl,File Filename)
        {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(filename);

            try 
            {
                MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                entity.addPart("file", new FileBody(Filename));
                entity.addPart("video-title", new StringBody("Video"));
                entity.addPart("video-type", new StringBody("1"));
                httpPost.setEntity(entity);

                HttpContext context = new BasicHttpContext();
                // Bind custom cookie store to the local context
                context.setAttribute(ClientContext.COOKIE_STORE, Globals.sessionCookie);

                HttpResponse response = httpClient.execute(httpPost, context);
                HttpEntity resEntity = response.getEntity();  
                String Response = "";
                if (response != null) 
                {    
                    Response = EntityUtils.toString(resEntity); 
                }
                return Response;
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }

            return "Exception";
        }