[英]415 Error while uploading png image on restful webservice
我正在嘗試上傳png圖像(然后從另一個客戶端下載)。 我正在獲取Http狀態碼415:我的錯誤是什么?
我已經在這里和Google上閱讀了所有內容,但仍未解決。
這是我的服務器代碼:
@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();
}
我假設您正在使用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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.