微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

RESTEasy 中的 GET 尚未上传的文件如何使用?

如何解决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);
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。