REST方法调用上载损坏的文件 [英] Corrupted files uploaded on REST method call

查看:66
本文介绍了REST方法调用上载损坏的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在Web应用程序中使用以下Rest方法上传文件.当我上传文本文件时,它们会正确保存,我可以打开它们.但是,如果使用其他任何格式,例如* .docx或* .pdf或* .jpg,则文件会以与原始文件相同的大小存储,但会损坏.以下是代码:

So I am using following Rest method in my web application to upload files. When I upload Text files, they are saved properly and i can open them. But in case of any other format i.e. *.docx or *.pdf or *.jpg, the files are stored with exact size as original files but are corrupted. Following is the code:

@POST
@Consumes("multipart/form-data")
public Response readFile() throws IOException, ServletException {
    Part filePart = request.getPart("c");
    InputStream f = filePart.getInputStream();
    String l = null;
    DataInputStream ds = new DataInputStream(f);
    File file = new File("c:\\temp\\" + getSubmittedFileName(filePart));
    try {
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        while ((l = ds.readLine()) != null) {
            bw.write(l);
        }
        bw.flush();
        bw.close();
        return Response.status(201).entity("File Created").build();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return Response.status(500).build();
}

和html页面如下:

<form action="api/fetch" method="post" enctype="multipart/form-data">
        <input id="c" name="c" type="file" aria-required="true"><br/><br/>
        <button type="submit">Submit</button>
</form>

我认为除了此以外,还必须有另一种上传文件的方式.我已参考如何将文件上传到服务器使用JSP/Servlet吗?,但我认为它并没有说明处理文件扩展名.那么,我的代码出了什么问题?

I assume there must be another way to upload files rather than this. i have refereed to How to upload files to server using JSP/Servlet? but i assume it doesn't say anything about handling file extension. So, what is going wrong with my code?

推荐答案

我相信错误在这里

DataInputStream ds = new DataInputStream(f);
...
while ((l = ds.readLine()) != null) {

来自 DataInputStream.readLine Javadoc

此方法不能正确将字节转换为字符.

This method does not properly convert bytes to characters.

您应该使用FileInputStream而不是DataInputStream. FileInputStream将所有文件视为字节.除了提到的问题之外,readLine在从输入文件读取所有换行符时也会剥离.

You should use a FileInputStream instead of a DataInputStream. The FileInputStream treat all your files as bytes. Beside the mentioned problem readLine would also strip during reading all linebreaks from your input file.

编辑有关演示,请参见下面的小片段.

edit For demonstration see below small snippet.

文件dummy.txt包含

foo
bar

foo之后的换行符是单个\n.在十六进制转储中是

The linebreak after foo is a single \n. In a hex dump it is

66 6F 6F 0A 62 61 72

现在使用DataInputStream读取一次文件,然后使用FileInputStream

Now the file is read once using a DataInputStream and once with a FileInputStream

try (DataInputStream ds = new DataInputStream(new FileInputStream("dummy.txt"));
        Writer bw = new BufferedWriter(new FileWriter("out_writer.txt"))) {
    String l;
    while ((l = ds.readLine()) != null) {
        bw.write(l);
    }
}
try (InputStream in = new FileInputStream("dummy.txt");
        OutputStream out = new FileOutputStream("out_inputstream.txt")) {
    byte[] buffer = new byte[8192];
    int readBytes = -1;
    while ((readBytes = in.read(buffer)) > -1) {
        out.write(buffer, 0, readBytes);
    }
}

输出文件是

out_writer.txt

ASCII: foobar
hex  : 66 6F 6F 62 61 72

out_inputstream.txt

ASCII: foo
       bar
hex  : 66 6F 6F 0A 62 61 72

如您所见,在DataInputStream示例中删除了0A(\n).尖刻的line break会使您的输出文件乱码.

As you can see the 0A (the \n) is stripped in the DataInputStream example. And this stipped line break garbles your output files.

这篇关于REST方法调用上载损坏的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆