1

I have an api in quarkus java when I upload the file I can't get the original name of it. When I use File.getName the return I get is this pfx16311440985252189274sfx

 `
package br.com.upload.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

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 org.jboss.resteasy.annotations.providers.multipart.MultipartForm;

import br.com.upload.entity.MultipartBody;

@Path("upload")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class UploadController {

    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public String sendUploada(@MultipartForm MultipartBody data) throws IOException {

        InputStream is = new BufferedInputStream(new FileInputStream(data.file));

        // String mimeType = URLConnection.guessContentTypeFromStream(is);

        // Long size = data.file.length();

        System.out.println("data file " + data.file.getName());

        Files.copy(is,
                new File("/home/allanfenx/Público/Java/upload/src/main/resources/upload",
                        "mai.png")
                                .toPath(),
                StandardCopyOption.REPLACE_EXISTING);

        return data.fileName;
    }
}`


`package br.com.upload.entity;

import java.io.File;

import javax.ws.rs.FormParam;
import javax.ws.rs.core.MediaType;

import org.jboss.resteasy.annotations.providers.multipart.PartType;

public class MultipartBody {

    @FormParam("file")
    @PartType(MediaType.APPLICATION_OCTET_STREAM)
    public File file;

    @FormParam("fileName")
    @PartType(MediaType.APPLICATION_JSON)
    public String fileName;

}`

This is my code quarkus uses InputStream but I'm using File. The code works perfectly but I can't get the original file name. The return I have is pfx16311440985252189274sfx it looks like they are characters to be decoded

1 Answers1

1

I've been through this a few days ago.

Unfortunately using Resteasy annotation, there is no way to get the filename directly. You have to parse the filename from request header 'Content-disposition', something like you can find here : @MultipartForm How to get the original file name?

However something's not mentionned: you can use 'quarkus-resteasy-reactive' implementation as explained here : https://quarkus.io/guides/resteasy-reactive You'll find a nice FileUpload class that can give you what you want. WARNING though: changing JAX-RS implementation to reactive might impact your entire app so I advise you to read quarkus documentation if you do that :)

camb
  • 105
  • 1
  • 12
  • If I use quarkus-resteasy-reactive will my entire application be reactive? no more thanks – Allan Da Silva Feb 23 '22 at 15:25
  • Only your JAX-RS related processes (managing RESTful Web Services for instance). If you want a full reactive app you should use reactive implementations of all your tools (ex: hibernate-reactive). Whatever your decision, maybe Multipart management shouldn't be driving it :) – camb Feb 23 '22 at 23:18