Quarkus 多文件上传
Quarkus Multiple File Upload
您好,我正在尝试使用多部分形式上传多个文件
我使用这个但我得到错误的请求状态,我如何上传多个文件?
public class AttachmentBody {
@FormParam("files")
@PartType(MediaType.APPLICATION_OCTET_STREAM)
public InputStream[] files;
}
我正在做一个部分,我认为它对多个文件上传有帮助。我正在使用 RestEasy 和 Quarkus 框架。找到下面的代码。
import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import org.apache.commons.io.IOUtils;
import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;
import org.jboss.resteasy.plugins.providers.multipart.InputPart;
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
@Path("/multiupload")
public class MultiFileUploadController {
private static String UPLOAD_DIR = "E:/sure-delete";
@POST
@Path("/files")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public Response handleFileUploadForm(@MultipartForm MultipartFormDataInput input) {
Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
List<String> fileNames = new ArrayList<>();
List<InputPart> inputParts = uploadForm.get("file");
System.out.println("inputParts size: " + inputParts.size());
String fileName = null;
for (InputPart inputPart : inputParts) {
try {
MultivaluedMap<String, String> header = inputPart.getHeaders();
fileName = getFileName(header);
fileNames.add(fileName);
System.out.println("File Name: " + fileName);
InputStream inputStream = inputPart.getBody(InputStream.class, null);
byte[] bytes = IOUtils.toByteArray(inputStream);
File customDir = new File(UPLOAD_DIR);
fileName = customDir.getAbsolutePath() + File.separator + fileName;
Files.write(Paths.get(fileName), bytes, StandardOpenOption.CREATE_NEW);
} catch (Exception e) {
e.printStackTrace();
}
}
String uploadedFileNames = String.join(", ", fileNames);
return Response.ok().entity("All files " + uploadedFileNames + " successfully.").build();
}
private String getFileName(MultivaluedMap<String, String> header) {
String[] contentDisposition = header.getFirst("Content-Disposition").split(";");
for (String filename : contentDisposition) {
if ((filename.trim().startsWith("filename"))) {
String[] name = filename.split("=");
String finalFileName = name[1].trim().replaceAll("\"", "");
return finalFileName;
}
}
return "unknown";
}
}
要从邮递员客户端进行测试,请在图片下方找到。
你可以以此为例也处理异常
您好,我正在尝试使用多部分形式上传多个文件
我使用这个但我得到错误的请求状态,我如何上传多个文件?
public class AttachmentBody {
@FormParam("files")
@PartType(MediaType.APPLICATION_OCTET_STREAM)
public InputStream[] files;
}
我正在做一个部分,我认为它对多个文件上传有帮助。我正在使用 RestEasy 和 Quarkus 框架。找到下面的代码。
import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import org.apache.commons.io.IOUtils;
import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;
import org.jboss.resteasy.plugins.providers.multipart.InputPart;
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
@Path("/multiupload")
public class MultiFileUploadController {
private static String UPLOAD_DIR = "E:/sure-delete";
@POST
@Path("/files")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public Response handleFileUploadForm(@MultipartForm MultipartFormDataInput input) {
Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
List<String> fileNames = new ArrayList<>();
List<InputPart> inputParts = uploadForm.get("file");
System.out.println("inputParts size: " + inputParts.size());
String fileName = null;
for (InputPart inputPart : inputParts) {
try {
MultivaluedMap<String, String> header = inputPart.getHeaders();
fileName = getFileName(header);
fileNames.add(fileName);
System.out.println("File Name: " + fileName);
InputStream inputStream = inputPart.getBody(InputStream.class, null);
byte[] bytes = IOUtils.toByteArray(inputStream);
File customDir = new File(UPLOAD_DIR);
fileName = customDir.getAbsolutePath() + File.separator + fileName;
Files.write(Paths.get(fileName), bytes, StandardOpenOption.CREATE_NEW);
} catch (Exception e) {
e.printStackTrace();
}
}
String uploadedFileNames = String.join(", ", fileNames);
return Response.ok().entity("All files " + uploadedFileNames + " successfully.").build();
}
private String getFileName(MultivaluedMap<String, String> header) {
String[] contentDisposition = header.getFirst("Content-Disposition").split(";");
for (String filename : contentDisposition) {
if ((filename.trim().startsWith("filename"))) {
String[] name = filename.split("=");
String finalFileName = name[1].trim().replaceAll("\"", "");
return finalFileName;
}
}
return "unknown";
}
}
要从邮递员客户端进行测试,请在图片下方找到。
你可以以此为例也处理异常