Pdf下载编码错误

时间:2016-06-01 15:19:38

标签: angularjs spring pdf typescript oracle-sqldeveloper

我已将pdf文件上传到我的数据库,现在我又想再次下载。
上传部分应该没问题,因为当我进入oracle sql developper时,我可以下载它并显示我的原始文本。

此刻我可以下载文件并打开它而不会出现错误,但内容已经消失,只是一个空的pdf。
Example
What it should be

我已经编写了一个自定义控制器来返回我的pdf

@RequestMapping(path = "/{id}/rapport", method = RequestMethod.GET)
public HttpEntity<?> getFile(@PathVariable("id") Long id) {
    Resource<String> resource = null;
    try {
        String file = new String(dossierRepository.findOne(id).getRapport(), "UTF-8");
        resource = new Resource<>(file);
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(DossierController.class.getName()).log(Level.SEVERE, null, ex);
    }
    return ResponseEntity.ok(resource);
}

然后我使用typescript / angular / ngFileSaver使用以下函数下载它。

public openFunc(item: any, column: any): void {
    if(column === "rapport"){
        item.customGET("rapport").then(
            (data: any): void => {
                let blob:Blob = new Blob([data.content], { type: 'application/pdf' });
                this.FileSaver.saveAs(blob, 'rapport.pdf');
            }
        );
    }
}

我在其他问题上发现pdf是一个多部分文件,其中一些内容采用其他格式,但不能像我在Oracle中那样下载数据SQL开发者?

我认为编码是错误的,但是如果我看oracle's blob mapping page它说blob是用UTF-8编码的,所以我不知道我做错了什么。

修改

我的getFile现在看起来像这样检查资源返回一个有效的文件(它没有)

@RequestMapping(path = "/{id}/rapport", method = RequestMethod.GET)
public HttpEntity<?> getFile(@PathVariable("id") Long id) {
    Resource<String> resource = null;
    try {
        String file = new String(dossierRepository.findOne(id).getRapport(), "UTF-8");
        resource = new Resource<>(file);
        BufferedWriter writer = null;
        try
        {
            writer = new BufferedWriter( new FileWriter("C:\\Users\\Sam\\files\\Masterproef lca\\lca-analysebeheer\\Test-Files\\rapporttest.pdf"));
            writer.write(resource.getContent());
        }
        catch (IOException e)...

只是写字符串也没有给我正确的文件(UTF-8或空,两个错误的结果)。

@RequestMapping(path = "/{id}/rapport", method = RequestMethod.GET)
public HttpEntity<?> getFile(@PathVariable("id") Long id) {
    Resource<String> resource = null;
    try {
        String file = new String(dossierRepository.findOne(id).getRapport(), "UTF-8");
        resource = new Resource<>(file);
        BufferedWriter writer = null;
        try
        {
            writer = new BufferedWriter( new FileWriter("C:\\Users\\Sam\\files\\Masterproef lca\\lca-analysebeheer\\Test-Files\\rapporttest.pdf"));
            writer.write(file);
        }
        catch (IOException e)...

尝试写出字节[]

@RequestMapping(path = "/{id}/rapport", method = RequestMethod.GET)
public HttpEntity<?> getFile(@PathVariable("id") Long id) {
    Resource<byte[]> resource = null;
    try {    
        resource = new Resource<>(dossierRepository.findOne(id).getRapport());

        Path path;
        path = Paths.get("C:\\Users\\Sam\\files\\Masterproef lca\\lca-analysebeheer\\Test-Files\\rapportTestBytes.pdf");
        Files.write(path, resource.getContent());

    } catch (IOException ex) {

给出了following不可读的编码字节的pdf。

我在将文件写入数据库

之前尝试编写该文件
String postRapport(@PathVariable("id") Long id, @RequestParam("rapport") MultipartFile file) {
    System.out.println("Entered custom file upload with id " + id + " and name " + file.getName());
    if (!file.isEmpty()) {
        BufferedWriter writer = null;

        try {
            byte[] bytes = file.getBytes();
            Dossier dossier = dossierRepository.findOne(id);
            dossier.setRapport(bytes);

            dossierRepository.save(dossier);
            Path path;
            path = Paths.get("C:\\Users\\Sam\\files\\Masterproef lca\\lca-analysebeheer\\Test-Files\\uploaddatabytesS.pdf");
            Files.write(path, bytes);
            return "You successfully uploaded " + file.getName() + " into " + file.getName() + "-uploaded !";
        } catch (Exception e)

这返回了一个正确的pdf文件,因此它不会写我的文件,我错了。 我真的不知道该怎么做,所以任何帮助都会被贬低。

0 个答案:

没有答案
相关问题