首页 > 解决方案 > 如何在 RESTEasy 中使用 GET 处理尚未上传的文件?

问题描述

我使用 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);
    }
}

标签: javaresteasyquarkusquarkus-rest-client

解决方案


推荐阅读