Status de download do arquivo ao usar o estilo repousante: 404

Status de download do arquivo ao usar o estilo repousante: 404

Cenário 404:
use o estilo restful quando o primeiro plano passar valores pelo url

<th><a href="fileDownLoad/${stu.filename}/${stu.filetype}">下载</a></th>

Método de recebimento em segundo plano:

 @RequestMapping("/fileDownLoad/{filename}/{filetype1}/{filetype2}")
  public void fileDownLoad(@PathVariable String filename,@PathVariable String filetype, HttpServletRequest req, HttpServletResponse resp) throws Exception{
    
    
 		
 		//验证传值信息
        System.out.println(filename + "-----------" + filetype);

        //把需要下载的文件从服务器读过来
        String realPath = req.getServletContext().getRealPath("/upload");
        File file = new File(realPath + "/" + filename);
        InputStream inputStream = new FileInputStream(file);

//        设置属性下载到本地
//        设置下载文件的长度
        resp.setContentLength((int) file.length());

//         设置下载文件的类型
        resp.setContentType(filetype);

//        设置响应头信息
        resp.setHeader("Content-Disposition","attachment;filename=" + filename);

        //把读取的文件写到本地
        OutputStream outputStream = resp.getOutputStream();
        IOUtils.copy(inputStream,outputStream);

        outputStream.close();
        inputStream.close();

    }

Verificação de saída:
System.out.println (nome do arquivo + "-----------" + tipo de arquivo);

8762a55b-80bd-47bb-b1ed-992f29751a66.jpg ----------- image / jpeg

Pode-se ver que o valor do tipo de arquivo é imagem / jpeg

No entanto, nosso método de aquisição:

public void fileDownLoad(@PathVariable String filename,@PathVariable String filetype, HttpServletRequest req, HttpServletResponse resp) throws Exception{
    
    

@PathVariable String filetype = image

Resposta correta:

public void fileDownLoad(@PathVariable String filename,@PathVariable String filetype1,@PathVariable String filetype2, HttpServletRequest req, HttpServletResponse resp) throws Exception{
    
    
    //public void fileDownLoad(String filename,String filetype, HttpServletRequest req,HttpServletResponse resp) throws Exception{
    
    
	String filetype = filetype1 + "/" +filetype2;

Use o método get regular:

Mesa da frente:

<th><a href="fileDownLoad?filename=${
    
    stu.filename}&filetype=${
    
    stu.filetype}">下载</a></th>

Nos bastidores:

@RequestMapping("fileDownLoad")
public void fileDownLoad(String filename,String filetype, HttpServletRequest req,HttpServletResponse resp) throws Exception{
    
    

Acho que você gosta

Origin blog.csdn.net/weixin_44192389/article/details/106976402
Recomendado
Clasificación