データベースにアップロードしたpdfファイルを投稿した項目(ID)のページごとにダウンロードできるようにしたい
サーブレットを用いてpdfファイル管理アプリを制作しております。
pdfファイルをアップロードすることはできたのですが、投稿した項目のページごとにファイルをダウンロードできるようにしたいです。
発生している問題・エラーメッセージ
現在の仕様だと、どのページでも同じpdfファイルのダウンロードになってしまいます。
Java
1@WebServlet("/reports/download") 2public class ReportsDownloadServlet extends HttpServlet { 3 private static final long serialVersionUID = 1L; 4 5 public ReportsDownloadServlet() { 6 super(); 7 } 8 9 protected void doPost(HttpServletRequest request, HttpServletResponse response) 10 throws ServletException, IOException { 11 OutputStream out = null; 12 InputStream in = null; 13 try { 14 File file = new File("/Applications/Eclipse_4.6.3.app/Contents/workspace/academic_management/WebContent/WEB-INF/new/ファイル名.pdf"); 15 response.setContentType("application/pdf"); 16 response.setHeader("Content-disposition", "attachment; filename="+file.getName()); 17 in = new FileInputStream(file); 18 out = response.getOutputStream(); 19 byte[] buff = new byte[1024]; 20 int len = 0; 21 while ((len = in.read(buff, 0, buff.length)) != -1) { 22 out.write(buff, 0, len); 23 } 24 } catch (Exception e){ 25 e.printStackTrace(); 26 } 27 finally { 28 29 if (in != null) { 30 try { 31 in.close(); 32 } catch (IOException e) { 33 } 34 } 35 if (out != null) { 36 try { 37 out.close(); 38 } catch (IOException e) { 39 } 40 } 41 } 42 } 43}
Java
1package models; 2 3import java.sql.Timestamp; 4 5import javax.persistence.Column; 6import javax.persistence.Entity; 7import javax.persistence.GeneratedValue; 8import javax.persistence.GenerationType; 9import javax.persistence.Id; 10import javax.persistence.NamedQueries; 11import javax.persistence.NamedQuery; 12import javax.persistence.Table; 13 14@Table(name = "reports") 15@NamedQueries({ 16 @NamedQuery( 17 name = "getAllReports", 18 query = "SELECT r FROM Report AS r ORDER BY r.id DESC" 19 ), 20 @NamedQuery( 21 name = "getReportsCount", 22 query = "SELECT COUNT(r) FROM Report AS r" 23 ), 24 @NamedQuery( 25 name = "checkRegisteredName", 26 query = "SELECT COUNT(r) FROM Report AS r WHERE r.name = :name" 27 ), 28}) 29@Entity 30public class Report { 31 @Id 32 @Column(name = "id") 33 @GeneratedValue(strategy = GenerationType.IDENTITY) 34 private Integer id; 35 36 @Column(name = "name", nullable = false, unique = true) 37 private String name; 38 39 @Column(name = "author", nullable = false) 40 private String author; 41 42 @Column(name = "year", nullable = false) 43 private String year; 44 45 @Column(name = "magazine", nullable = false) 46 private String magazine; 47 48 @Column(name = "created_at", nullable = false) 49 private Timestamp created_at; 50 51 @Column(name = "department", nullable = false) 52 private Integer department; 53 54 // 55 @Column(name = "data", nullable = false) 56 private String data; 57 58 @Column(name = "updated_at", nullable = false) 59 private Timestamp updated_at; 60 61 @Column(name = "delete_flag", nullable = false) 62 private Integer delete_flag; 63 64 public Integer getId() { 65 return id; 66 } 67 68 public void setId(Integer id) { 69 this.id = id; 70 } 71 72 public String getName() { 73 return name; 74 } 75 76 public void setName(String name) { 77 this.name = name; 78 } 79 80 public String getAuthor() { 81 return author; 82 } 83 84 public void setAuthor(String author) { 85 this.author = author; 86 } 87 88 public String getYear() { 89 return year; 90 } 91 92 public void setYear(String year) { 93 this.year = year; 94 } 95 96 public String getMagazine() { 97 return magazine; 98 } 99 100 public void setMagazine(String magazine) { 101 this.magazine = magazine; 102 } 103 104 public Integer getDepartment() { 105 return department; 106 } 107 108 public void setDepartment(Integer department) { 109 this.department = department; 110 } 111 112 public String getData() { 113 return data; 114 } 115 116 public void setData(String data) { 117 this.data = data; 118 } 119 120 public Timestamp getCreated_at() { 121 return created_at; 122 } 123 124 public void setCreated_at(Timestamp created_at) { 125 this.created_at = created_at; 126 } 127 128 public Timestamp getUpdated_at() { 129 return updated_at; 130 } 131 132 public void setUpdated_at(Timestamp updated_at) { 133 this.updated_at = updated_at; 134 } 135 136 public Integer getDelete_flag() { 137 return delete_flag; 138 } 139 140 public void setDelete_flag(Integer delete_flag) { 141 this.delete_flag = delete_flag; 142 } 143}
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。