我在使用 Java HttpServlet、Hibernate(3.5) 和 Postgresql Server(9.1.9) 实现文件上传/下载时遇到问题,一般来说,问题是当我通过以下方式上传文件时:
FileItemFactory factory = new DiskFileItemFactory();
ServletContext servletContext = this.getServletConfig().getServletContext();
File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
((DiskFileItemFactory) factory).setRepository(repository);
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List<FileItem> items = upload.parseRequest(request);
byte[] dataBytes = items.get(0).get();
Session session = (Session) entityManager.getDelegate();
SQLQuery query = session
.createSQLQuery("SELECT nextval('test_vorgabe.seq_did')");
Long key = ((BigInteger) query.uniqueResult()).longValue();
session.flush();
Datei datei = new Datei();
datei.setDid(key);
datei.setRevision(new Date(System.currentTimeMillis()));
datei.setFilename(items.get(0).getName()
);
datei.setFile(dataBytes);
session.save(datei);
session.flush();
} catch (FileUploadException e) {
e.printStackTrace();
}
这似乎工作正常(文件大小也与我的计算机上的相同)但问题是,当我下载它时 - 文件大小是原来的两倍并且文件不起作用当我将其发送回客户时不再。
String dateiname = request.getReader().readLine();
System.out.println("Dateiname: " + dateiname);
dateiname = dateiname.replace("isc_TextItem_", "");
dateiname = dateiname.substring(2);
while (dateiname.startsWith("=")) {
dateiname = dateiname.substring(1);
}
System.out.println(dateiname);
Session session = (Session) entityManager.getDelegate();
Criteria crit = session.createCriteria(Datei.class);
crit.add(Restrictions.eq("filename", dateiname));
Datei datei = (Datei) crit.list().get(0);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-disposition", "attachment;filename=\""
+ datei.getFilename() + "\"");
ServletOutputStream stream = response.getOutputStream();
stream.write(datei.getFile());
stream.flush();
stream.close();
我还将包括实体 bean 的来源:
@Entity
@Table(name = "datei", schema = "test_vorgabe")
public class Datei implements java.io.Serializable {
private long did;
private Vorgabe vorgabe;
private String filename;
private byte[] file;
private Date revision;
public Datei() {
}
public Datei(long did, String filename, byte[] file, Date revision) {
this.did = did;
this.filename = filename;
this.file = file;
this.revision = revision;
}
public Datei(long did, Vorgabe vorgabe, String filename, byte[] file,
Date revision) {
this.did = did;
this.vorgabe = vorgabe;
this.filename = filename;
this.file = file;
this.revision = revision;
}
@Id
@Column(name = "did", unique = true, nullable = false)
public long getDid() {
return this.did;
}
public void setDid(long did) {
this.did = did;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "vid")
public Vorgabe getVorgabe() {
return this.vorgabe;
}
public void setVorgabe(Vorgabe vorgabe) {
this.vorgabe = vorgabe;
}
@Column(name = "filename", nullable = false)
public String getFilename() {
return this.filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
@Column(name = "file", nullable = false)
public byte[] getFile() {
return this.file;
}
public void setFile(byte[] file) {
this.file = file;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "revision", nullable = false, length = 29)
public Date getRevision() {
return this.revision;
}
public void setRevision(Date revision) {
this.revision = revision;
}
}
它在 postgresql 中保存为 bytea
有人可以帮我解决这个问题吗?
提前谢谢