Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 生成公共访问文件和访问该文件的临时url_Java_Spring Mvc - Fatal编程技术网

Java 生成公共访问文件和访问该文件的临时url

Java 生成公共访问文件和访问该文件的临时url,java,spring-mvc,Java,Spring Mvc,我在oracle数据库中有几个文件存储为Blob。我呈现一个列表,向用户显示存储的文件,我想给他一个下载每个文件的链接。因此,我在考虑一个函数,它允许我从数据库中存储的字节流生成一个临时文件,并生成一个临时url,让用户下载该文件。伪代码中的方式如下所示: File tempFile = generateTempFileFrom(bytes[]); String tempUrl = generateTempUrlFromTempFile(tempFile); 所以我可以做 <a href

我在oracle数据库中有几个文件存储为Blob。我呈现一个列表,向用户显示存储的文件,我想给他一个下载每个文件的链接。因此,我在考虑一个函数,它允许我从数据库中存储的字节流生成一个临时文件,并生成一个临时url,让用户下载该文件。伪代码中的方式如下所示:

File tempFile = generateTempFileFrom(bytes[]);
String tempUrl = generateTempUrlFromTempFile(tempFile);
所以我可以做

<a href="tempUrl">filename</a>

正如我所说的,这是伪代码,只是想让你知道我想做什么。

早上好

您不需要创建任何文件,只需将BLOB转换为字节数组并将其发送回web客户端即可

例如,使用Spring 4: 我在GET/download/{id}上创建了一个控制器

@Controller
public class BlobController {

    @RequestMapping("/download/{id}")
    public HttpEntity<byte[]> download(@PathVariable String id) {

        // JDBC code to retrieve the BLOB column
        stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT FILENAME, FILETYPE, CONTENT FROM MY TABLE WHERE ID = " + id);
        Blob blob = rs.getBlob("CONTENT");
        String fileName= rs.getString("FILENAME"); // ex. file1
        String fileType= rs.getString("FILETYPE"); // ex. pdf

        // Transform BLOB to Byte array
        int blobLength = (int) blob.length();  
        byte[] document = blob.getBytes(1, blobLength);


        // Set HTTP Header to force the browser to open the download popup
        HttpHeaders header = new HttpHeaders();
        header.setContentType(new MediaType("application", fileType));
        header.set("Content-Disposition", "attachment; filename=" + file1 + "." + fileType);
        header.setContentLength(blobLength );

        //release the blob and free up memory. (since JDBC 4.0)
        blob.free();

        return new HttpEntity<byte[]>(document, header);
    }
}
@控制器
公共类BlobController{
@请求映射(“/download/{id}”)
公共HttpEntity下载(@PathVariable字符串id){
//用于检索BLOB列的JDBC代码
stmt=connection.createStatement();
ResultSet rs=stmt.executeQuery(“从我的表中选择文件名、文件类型、内容,其中ID=“+ID”);
Blob Blob=rs.getBlob(“内容”);
String fileName=rs.getString(“fileName”);//例如file1
String fileType=rs.getString(“fileType”);//ex.pdf
//将BLOB转换为字节数组
int blobLength=(int)blob.length();
byte[]document=blob.getBytes(1,blobLength);
//设置HTTP标头以强制浏览器打开下载弹出窗口
HttpHeaders header=新的HttpHeaders();
setContentType(新媒体类型(“应用程序”,文件类型));
header.set(“内容处置”、“附件;文件名=“+file1+””+fileType);
header.setContentLength(blobLength);
//释放blob并释放内存。(自JDBC4.0以来)
blob.free();
返回新的HttpEntity(文档,标题);
}
}
然后,您应该能够使用此HTML代码下载您的文件

<a href="/download/1">donwload file 1</a>
<a href="/download/2">donwload file 2</a>

希望这是清楚的


祝你好运

非常感谢,我没有意识到这个解决方案!我现在就去试试