@POST
@Path("/upload")
@Consumes("multipart/form-data")
public Response uploadFile(MultipartFormDataInput input) {
String fileName = "";
Map<String, List<InputPart>> formParts = input.getFormDataMap();
List<InputPart> inPart = formParts.get("file"); // "file" should match the name attribute of your HTML file input
for (InputPart inputPart : inPart) {
try {
// Retrieve headers, read the Content-Disposition header to obtain the original name of the file
MultivaluedMap<String, String> headers = inputPart.getHeaders();
String[] contentDispositionHeader = headers.getFirst("Content-Disposition").split(";");
for (String name : contentDispositionHeader) {
if ((name.trim().startsWith("filename"))) {
String[] tmp = name.split("=");
fileName = tmp[1].trim().replaceAll("\"","");
}
}
// Handle the body of that part with an InputStream
InputStream istream = inputPart.getBody(InputStream.class,null);
/* ..etc.. */
}
catch (IOException e) {
e.printStackTrace();
}
}
String msgOutput = "Successfully uploaded file " + filename;
return Response.status(200).entity(msgOutput).build();
}
MultipartFormDataInput 获取图片名称
最新推荐文章于 2024-10-19 11:16:48 发布
本文介绍了一个使用Java实现的文件上传接口,该接口通过@Path(/upload)指定路径,并使用@Consumes(multipart/form-data)消费类型来处理多部分表单数据。具体实现了从客户端接收文件、读取文件名并返回上传成功消息的功能。

3万+

被折叠的 条评论
为什么被折叠?



