use*_*187 21 java rest json web-services
我想发送一个文件到webservice,但我需要发送更多的信息,所以我想用json发送它们.但是当我把一个文件放在我的jsonObject中时,我得到一个错误,说它不是一个字符串.我的问题是,我应该拿我的文件并转换为字符串,然后放入我的json和Web服务中取出它并将该字符串转换为文件?还是有另一种简单的方法吗?
这是我的代码:
客户:
private void send() throws JSONException{
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
client.addFilter(new LoggingFilter());
WebResource service = client.resource("http://localhost:8080/proj/rest/file/upload_json");
JSONObject my_data = new JSONObject();
File file_upload = new File("C:/hi.txt");
my_data.put("User", "Beth");
my_data.put("Date", "22-07-2013");
my_data.put("File", file_upload);
ClientResponse client_response = service.accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, my_data);
System.out.println("Status: "+client_response.getStatus());
client.destroy();
}
Run Code Online (Sandbox Code Playgroud)
网络服务
@POST
@Path("/upload_json")
@Consumes(MediaType.APPLICATION_JSON)
@Produces("text/plain")
public String receive(JSONObject json) throws JSONException {
//Here I'll save my file and make antoher things..
return "ok";
}
Run Code Online (Sandbox Code Playgroud)
在所有答案之后,这是我的代码 - 谢谢大家:
网络服务
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import com.sun.jersey.core.util.Base64;
@Path("/file")
public class ReceiveJSONWebService {
@POST
@Path("/upload_json")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public JSONObject receiveJSON(JSONObject json) throws JSONException, IOException {
convertFile(json.getString("file"), json.getString("file_name"));
//Prints my json object
return json;
}
//Convert a Base64 string and create a file
private void convertFile(String file_string, String file_name) throws IOException{
byte[] bytes = Base64.decode(file_string);
File file = new File("local_path/"+file_name);
FileOutputStream fop = new FileOutputStream(file);
fop.write(bytes);
fop.flush();
fop.close();
}
}
Run Code Online (Sandbox Code Playgroud)
客户
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import javax.ws.rs.core.MediaType;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.LoggingFilter;
import com.sun.jersey.core.util.Base64;
import com.sun.jersey.multipart.FormDataMultiPart;
import com.sun.jersey.multipart.file.FileDataBodyPart;
import com.sun.jersey.multipart.impl.MultiPartWriter;
public class MyClient {
public static void main(String[] args) throws JSONException, IOException
{
MyClient my_client = new MyClient();
File file_upload = new File("local_file/file_name.pdf");
my_client.sendFileJSON(file_upload);
}
private void sendFileJSON(File file_upload) throws JSONException, IOException{
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
client.addFilter(new LoggingFilter());
WebResource service = client.resource("my_rest_address_path");
JSONObject data_file = new JSONObject();
data_file.put("file_name", file_upload.getName());
data_file.put("description", "Something about my file....");
data_file.put("file", convertFileToString(file_upload));
ClientResponse client_response = service.accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, data_file);
System.out.println("Status: "+client_response.getStatus());
client.destroy();
}
//Convert my file to a Base64 String
private String convertFileToString(File file) throws IOException{
byte[] bytes = Files.readAllBytes(file.toPath());
return new String(Base64.encode(bytes));
}
}
Run Code Online (Sandbox Code Playgroud)
您应该将文件数据转换为Base64编码然后传输它,例如:
byte[] bytes = Files.readAllBytes(file_upload.toPath());
dados.put("File", DatatypeConverter.printBase64Binary(bytes));
Run Code Online (Sandbox Code Playgroud)
我不知道dados指的是什么,可能是 a Map<String, String>,但我认为您想使用JSONObject刚刚创建的
JSONObject my_data = new JSONObject();
File file_upload = new File("C:/hi.txt");
my_data.put("User", "Beth");
my_data.put("Date", "22-07-2013");
my_data.put("File", file_upload);
Run Code Online (Sandbox Code Playgroud)
但这是无用的,并且可能不会按照您的想法进行。对象File不保存文件,它保存文件的路径,即。C:/hi.txt。如果这就是您放入 JSON 中的内容,它将生成
{"File" : "C:/hi.txt"}
Run Code Online (Sandbox Code Playgroud)
它不会包含文件内容。
所以你不妨直接输入文件路径
JSONObject my_data = new JSONObject();
my_data.put("User", "Beth");
my_data.put("Date", "22-07-2013");
my_data.put("File", "C:/hi.txt");
Run Code Online (Sandbox Code Playgroud)
如果您尝试使用 JSON 进行文件上传,一种方法是使用 Java 7 的 NIO 从文件中读取字节
byte[] bytes = Files.readAllBytes(file_upload .toPath());
Run Code Online (Sandbox Code Playgroud)
Base64 对这些字节进行编码并将它们作为字符串写入 JSONObject 中。使用 Apache Commons 编解码器
Base64.encodeBase64(bytes);
my_data.put("File", new String(bytes));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
58776 次 |
| 最近记录: |