如何下载pdf文件并在Spring mvc中重定向到主页

来源:爱站网时间:2021-09-16编辑:网友分享
以下代码运行正常,没有错误,但没有按要求运行。.问题1:每当我导航到url(。时,我都想下载pdf文件并重定向到首页(url:../)。 ./admin / ...

问题描述


以下代码运行正常,没有错误,但未按要求运行。.

问题1:我想下载pdf文件并重定向到首页页面(url:../)。当我浏览到url(../ admin / generate_pdf)

问题2:当我在Pdfdemo.java中取消注释行时,给我一个错误404页面未找到。

Pdfdemo.java

public class Pdfdemos {
    private static String USER_PASSWORD = "password";
    private static String OWNER_PASSWORD = "lokesh";

        public String generate_pdf() {
            try {
                String file_name="d:\\sanjeet7.pdf";
                Document document= new Document();
                PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream(file_name));

//              writer.setEncryption(USER_PASSWORD.getBytes(),
//                          OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING |PdfWriter.ALLOW_ASSEMBLY|
//                          PdfWriter.ALLOW_COPY|
//                          PdfWriter.ALLOW_DEGRADED_PRINTING|
//                          PdfWriter.ALLOW_FILL_IN|
//                          PdfWriter.ALLOW_MODIFY_ANNOTATIONS|
//                          PdfWriter.ALLOW_MODIFY_CONTENTS|
//                          PdfWriter.ALLOW_SCREENREADERS|
//                          PdfWriter.ALLOW_ASSEMBLY|
//                          PdfWriter.ENCRYPTION_AES_128, 0);


                document.open();
                document.add(new Paragraph(" "));document.add(new Paragraph(" "));
                String days_in_week[]= {"monday","tuesday","webnesday","thursday","friday","saturday"};
                int period=8;
                String user="springstudent";
                String pass="springstudent";
                String jdbcUrl="jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false";
                String driver="com.mysql.jdbc.Driver";
                Connection myconn=DriverManager.getConnection(jdbcUrl,user,pass);
                PreparedStatement ps=null;
                ResultSet rs=null;
                String query="select * from class_t";
                ps=myconn.prepareStatement(query);
                rs=ps.executeQuery();
                while(rs.next()) {
                    Paragraph para=new Paragraph("Time table for class"+rs.getString("class")+rs.getString("section"));
                    document.add(para);
                    System.out.println(rs.getInt("id"));
                    PdfPTable table=new PdfPTable(period+1);
                    for(int i=0;i

Generate_pdf_controller.java

@Controller
@RequestMapping("/admin/generate_pdf")
public class Generate_pdf_Controller {


    @GetMapping("/online")
    public String generating_pdf(Model theModel) {
        System.out.println("hello");
        CustomerServiceImpl pal=new CustomerServiceImpl();
        Pdfdemos pal1=new Pdfdemos();
        String xx=pal1.generate_pdf();
        return "redirect:/";
    }
}

思路:


只需获取pdf(从本地文件系统下载到浏览器中,此代码就足够了:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/admin/generate_pdf")
public class PdfController {

    @GetMapping(value = "/online", /*this is "nice to have" ->*/ produces = MediaType.APPLICATION_PDF_VALUE)
    @ResponseBody // this is important, as the return type byte[]
    public byte[] generating_pdf() throws IOException {//exception handling...
        System.out.println("hello");//logging
        // re-generate new file if needed (thread-safety!)...
        // ..and dump the content as byte[] response body:
        return Files.readAllBytes(Paths.get("d:\\sanjeet7.pdf"));
    }
}

...如果您需要“刷新”,则可以“重写”文件/重新调用服务,但仍然可能不是“线程安全的”解决方案。


如果您想“操作得更近字节”并且使用void返回类型/否@ResponseBody,我认为这应该仍然有效:

@Controller
public class ... {

   RequestMapping(value = ..., produces ...)
   //no request body, void return type, response will be autowired and can be handled
   public void generatePdf(javax.servlet.http.HttpServletResponse response) throws ... {
      java.io.OutputStream outStr = response.getOutputStream();
      // dump from "somewhere" into outStr, "finally" close.
      outStr.close();
   }
}

上一篇:覆盖Java中的hashCode()和equals()方法

下一篇:无法访问API-29中的外部存储空间

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载