为什么dropwizard不会上传xls文件?

时间:2016-07-17 07:37:39

标签: java io jersey dropwizard

我使用dropwizard制作了一个非常传统的文件上传方法。

所以我在资源中有一个像这个

开始的方法
@POST
@Path("/uploadFile")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@UnitOfWork
public Response uploadFile(
    @FormDataParam("file") final InputStream fileInputStream,
    @FormDataParam("file") final FormDataContentDisposition contentDispositionHeader, 
    @Context HttpServletRequest request) throws IOException {...

内部没什么特别的,它只是保存到一个路径,使用java.nio库,就像这样

java.nio.file.Path outputPath = FileSystems.getDefault()
    .getPath(System.getProperty("user.home"), fileName);

    if (!Files.exists(outputPath.getParent()))
        Files.createDirectories(outputPath.getParent());

    Files.copy(fileInputStream, outputPath);

它不会上传excel文件。我在别处读到excel文件及其基础类型被怀疑。我需要做什么?

1 个答案:

答案 0 :(得分:0)

基本上,该方法适用于其他文件,但不会上传xls文件。我不知道确切的原因,但我怀疑是在问题中发布的。我开始工作的方式是替换简单的复制线

Files.copy(fileInputStream, outputPath);

OutputStream outputStream = null;
    try {
        outputStream = new FileOutputStream(new File(outputPath.toString()));
        int read = 0;
        byte[] bytes = new byte[1024];
        while ((read = fileInputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }
        outputStream.flush();
    }
    catch (IOException ioe) {
        ioe.printStackTrace();
    }
    finally{
        outputStream.close();
    }

通过将输入流读入字节数组,然后将其写入新的FileOutputStream,可以解决(我怀疑是安全功能)。

如果任何人有“正确的方法”,那比这更好,看到它会很酷。堆栈跟踪并没有告诉我发生了什么。