问题描述
我使用 Quarkus 和 RESTEasy,遇到了一些棘手的问题,看起来很容易解决,但我的尝试是徒劳的。 我已经实现了一种使用 APPLICATION_FORM_DATA(完全是文本文件)的方法。保存效果很好,但现在我想将文件本身的文本输出到用户的 GET 请求。如何为每个用户正确指向上传的文本文件的路径?
FileUploadController(用于保存文件):
package TextGenerator;
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;
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 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;
@Path("/upload")
public class FileUploadController {
private static final String UPLOAD_DIR = "/home/binocla/IdeaProjects/getting-started/target/classes/data/files";
@POST
@Path("/file")
@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("=");
return name[1].trim().replaceAll("\"","");
}
}
return "unkNown";
}
}
CustomText GET 事件处理程序类:
package TextGenerator;
import TextGenerator.handlers.Files;
import io.smallrye.mutiny.Uni;
import lombok.extern.jbosslog.JBossLog;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.io.File;
@JBossLog
@Path("") // What path shall I use here?
public class CustomText {
public static final File CUSTOMTEXT = Files.getFile("*name from the path*.txt"); // custom uploaded file text
@Inject
protected Service service;
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("{length}/{depth}")
public Uni<String> customText(@PathParam("length") int length,@PathParam("depth") int depth) {
if (depth > 2) {
depth = 4;
}
if (length > 20000) {
length = 20000;
}
log.debug("Current depth of customText is " + depth);
log.debug("Current length of customText is " + length);
return service.source(CUSTOMTEXT,length,depth);
}
@GET
@Produces(MediaType.TEXT_PLAIN)
public Uni<String> customText() {
log.debug("No Params customText");
return service.source(CUSTOMTEXT,10,1);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)