Java 请使用POST接受文件参数和字符串

Java 请使用POST接受文件参数和字符串,java,http,file-upload,glassfish,jersey,Java,Http,File Upload,Glassfish,Jersey,我正在使用Jersey开发一个rest服务器。我需要制作一个API,将图像上传到服务器上的文件夹中。我的API必须包含2个参数。字符串参数和文件参数 @POST @Path("path") @public Response uploadImage(@FormParam("name") String name, @FormDataParam("imagestream") InputStream imageStream) 如果我从客户端应用程序发送2个参数,我无法同时接收这两个属性 我的客户端代码

我正在使用Jersey开发一个rest服务器。我需要制作一个API,将图像上传到服务器上的文件夹中。我的API必须包含2个参数。字符串参数和文件参数

@POST
@Path("path")
@public Response uploadImage(@FormParam("name") String name, @FormDataParam("imagestream") InputStream imageStream)
如果我从客户端应用程序发送2个参数,我无法同时接收这两个属性

我的客户端代码如下所示:

private static void instantiateConnection(String urlString, byte[] params)
{
    HttpURLConnection conn = null;

    try
    {
        URL url = new URL(urlString);

        conn = (HttpURLConnection) url.openConnection();

        outStream(conn, params);

        int responseCode = conn.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK)
        {
            InputStream instream = conn.getInputStream();

            instream.close();
        }

    }
    catch (MalformedURLException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        conn.disconnect();
    }
}

private static void outStream(HttpURLConnection conn, byte[] param)
{
    String name = new String("name");

    conn.addRequestProperty("Content-Length", "" + (param.length + name.length()));
    conn.addRequestProperty("Content-Type", "multipart/mixed");

    try
    {
        conn.setRequestMethod("POST");
        conn.setUseCaches(false);
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setReadTimeout(15000);


        // Send request
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
        wr.write(name.getBytes());
        wr.write(param);
        wr.flush();
        wr.close();
    }
    catch (ProtocolException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

    return;
}

我哪里错了?

这是我使用的一段代码

@POST
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response newFile(@FormDataParam("file") InputStream uploadInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @FormDataParam("description") String description) {

    String uploadFileLocation = "C:/upload/documents/";

    OutputStream out = null;

    int read = 0;
    byte[] bytes = new byte[1024];

    File directory = new File(uploadFileLocation);
    if(!directory.exists()){
        directory.mkdirs();
    }
    out = new FileOutputStream(new File(directory, fileDetail.getFileName()));
    while ((read = uploadInputStream.read(bytes)) != -1) {
        out.write(bytes, 0, read);
    }

    out.flush();
    out.close();

//Return Response
...
}

您根本没有编写正确的
多部分/混合
请求正文。另外,
byte[]
数组应该代表什么?byte[]param属性代表一个图像。jpgIf我使用FormDataParam而不是FormData作为字符串属性,编译器给我一个错误