我正在尝试上传png图像(然后从其他客户端下载)。 我收到Http状态代码415:我的错误是什么?
我已经在谷歌和谷歌上阅读了所有内容,但仍未解决。
这是我的服务器代码:
@Path("/file")
public class UploadFileService {
@POST
@Path("/upload")
@Consumes("image/png")
public Response uploadPng(File file) throws IOException {
String uploadedFileLocation =
"C:/Users/Desktop/server/" + file.getName();
DataInputStream diStream =
new DataInputStream(new FileInputStream(file));
long len = (int) file.length();
byte[] fileBytes = new byte[(int) len];
int read = 0;
int numRead = 0;
while (read < fileBytes.length && (numRead =
diStream.read(fileBytes, read,
fileBytes.length - read)) >= 0) {
read = read + numRead;
}
// save it
writeToFile(diStream, uploadedFileLocation);
System.out.println("File uploaded to : " + uploadedFileLocation);
return Response.status(200).entity(file).build();
}
// save uploaded file to new location
private void writeToFile(InputStream uploadedInputStream,
String uploadedFileLocation) {
try {
OutputStream out =
new FileOutputStream(new File(uploadedFileLocation));
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(uploadedFileLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
这是客户端代码:
public class FileUploadClient {
public static void main(String[] args) {
try {
File fileToUpload = new File("C:/Users/client/file.png");
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource resource =
client.resource("http://localhost:8080/WS/rest/file/upload");
ClientResponse response =
resource.accept("image/png").post(ClientResponse.class, fileToUpload);
System.out.println(response.getStatus());
} catch (Exception e) {
e.printStackTrace();
}
}
}
修改 它的工作原理如下:
public class FileUploadClient{
public static void main(String[] args){
try{
File fileToUpload = new File("C:/Users/client/file.png");
InputStream is =new FileInputStream(fileToUpload);
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource resource = client.resource("http://localhost:8080/WS/rest/");
@SuppressWarnings("resource")
FormDataMultiPart part = new FormDataMultiPart().
field("file", is, MediaType.APPLICATION_OCTET_STREAM_TYPE);
String response = resource.path("file").path("upload").type(MediaType.MULTIPART_FORM_DATA_TYPE).post(String.class, part);
System.out.println(response);
}
catch(Exception e){
e.printStackTrace();
}
}
}
这就是POST。
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("file") InputStream uploadedInputStream){
//@FormDataParam("file") FormDataContentDisposition fileDetail) {
String uploadedFileLocation = "C:/Users/Desktop/server/file.png";
// save it
writeToFile(uploadedInputStream, uploadedFileLocation);
String output = "File uploaded to : " + uploadedFileLocation;
return Response.status(200).entity(output).build();
}
答案 0 :(得分:1)
我假设您使用CXF作为JAX-RS实现,然后在我看来,图像应该以内容类型作为多部分表单数据上传。
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
Response uploadImage(List<Attachment> attachments);
这里的附件是“org.apache.cxf.jaxrs.ext.multipart.Attachment”对象。
数据可以如下读取
for (final Attachment attachment : attachments) {
String fileName = attachment.getDataHandler().getName();
final InputStream inputStream = attachment.getDataHandler().getInputStream();
ByteStreams.copy(inputStream, outputStream);
inputStream.close();
final byte[] image = outputStream.toByteArray();
}