简体   繁体   English

png文件上传Restful Web Service Java

[英]png file Uploading Restful Web Service java

I try to write an image file uploading post method for my web service. 我尝试为我的Web服务编写一个图像文件上传发布方法。 Here is my post method. 这是我的发帖方法。 The image file can be uploaded into post method but can not converted into Image type. 图像文件可以上传到发布方法,但不能转换为图像类型。

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("text/html")
public Response uploadFile(@FormDataParam("file") File file2) throws IOException {
    InputStream IS = null;

    String output = "";

    try {
        IS = new FileInputStream(file2);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {

    }


    try {
        if (file2 != null && IS != null) {
            Image img = ImageIO.read(IS);
            if (img != null) {
                output = "image file transmission sucessed";

            } else {
                String out = convertStreamToString(IS);
                output = "file uploaded into post method, however can not transfer it into image type "+
                "\n"+ out;
            }

        } else if (file2 == null) {
            output = "the file uploaded into post method is null";
        }

    } catch (IOException e) {
        e.printStackTrace();

    }

    return Response.status(200).entity(output).build();

}

static String convertStreamToString(java.io.InputStream is) {
    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}

The "file uploaded into post method, however can not transfer it into image type "+"\\n"+ out; “文件已上传到发布方法中,但是无法将其传输到图像类型” +” \\ n” +中; message is shown. 显示信息。 The reason I believe is the inputStream contants extral file information + the content of the image. 我相信的原因是inputStream包含了Extral文件信息+图像的内容。 When I try to convert the inputStream back to Image, I need to find a way to get rid of the extra info passed. 当我尝试将inputStream转换回Image时,我需要找到一种方法来摆脱传递的额外信息。

here is my new reversion: 这是我的新版本:

@POST
@Path("/images")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response imageUpload(
        @FormDataParam("image") InputStream hereIsImage,
        @FormDataParam("image") FormDataContentDisposition hereIsName) {
    String path = "f://";
    if (hereIsName.getSize() == 0) {
        return Response.status(500).entity("image parameter is missing")
                .build();
    }
    String name = hereIsName.getFileName();
    path += name;

    try {
        OutputStream out = new FileOutputStream(new File(path));
        int read;
        byte[] bytes = new byte[1024];
        while ((read = hereIsImage.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        out.flush();
        out.close();
    } catch (IOException e) {
        return Response.status(500)
                .entity(name + " was not uploaded\n" + e.getMessage())
                .build();
    }
    return Response.status(200).entity(name + " was uploaded").build();
}

However, when I upload the image, an error pops up: 但是,当我上传图像时,会弹出一个错误:

[ERROR] An exception occurred during processing of the au.edu.rmit.srtwebservice.util.rest.testWS class. [错误]在处理au.edu.rmit.srtwebservice.util.rest.testWS类期间发生异常。 This class is ignored. 此类被忽略。 com/sun/jersey/core/header/FormDataContentDisposition COM /阳光/汗布/核心/头/ FormDataContentDisposition

the testWS.calss is where my method is. testWS.calss是我的方法所在的位置。

public Response uploadFile(@FormDataParam("file") File file2)

File input may not work because you most likely be sending an octet stream from your client. File输入可能不起作用,因为您很可能从客户端发送一个八位位组流。 So try using InputStream as the input type and hopefully it should work. 因此,请尝试使用InputStream作为输入类型,并希望它应该可以工作。

public Response uploadFile(@FormDataParam("file") InputStream file2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM