使用MySQL存储和检索word文档 [英] Store and retrieve word documents with MySQL
问题描述
我需要使用Servlet将MS Word文档存储到MySQL 5.1中。我有代码上传一个文件,但我不知道我可以喂到表中。我已经使用了BLOB来插入.doc文件。
这里是我的代码片段来上传文件:
protected void doPost (HttpServletRequest请求,HttpServletResponse响应)
throws ServletException,IOException {
response.setContentType(text / html; charset = UTF-8);
PrintWriter out = response.getWriter();
try {
//获取从客户端上传的文件
Part p1 = request.getPart(file);
String type = p1.getContentType();
String name = p1.getName();
long size = p1.getSize();
InputStream is = p1.getInputStream();
// FileInputStream fis = is。
//读取作为零件发送的文件名
零件p2 = request.getPart(name);
扫描仪s =新扫描仪(p2.getInputStream());
String filename = s.nextLine(); //从流中读取文件名
//在服务器上使用文件名
String outputfile = this.getServletContext()。getRealPath(filename); //获取服务器上的路径
FileOutputStream os = new FileOutputStream(outputfile);
//将上传文件中的字节写入目标文件
int ch = is.read();
while(ch!= -1){
os.write(ch);
ch = is.read();
}
os.close();
out.println(< h3> File:'+ name +'Type:'+ type +'
+Size:+((double)size / 1024 )+KB上传成功!< / h3>);
catch(Exception ex){
out.println(Exception - >+ ex.getMessage());
}
finally {
out.close();
$ b $ p
$ b在这里,我已经使用了Servlets 3.0特性来上传一个文件...
我的表架构:
$ p $资源
UserID [varchar(15)]
- Document [中] b $ b
任何人都可以帮助我如何将文档存储到表,尽管BLOB是一种代表二进制数据的类型,我怎样才能以Word文档(* .doc)的形式检索?
将Word文档存储在文件中的部分答案:
您不需要任何额外的列来保存文件名,因为文档的记录ID可以用作文件名。
在保存新文档时,请在数据库事务中进行操作,以便在出现问题时撤消该进程。
在$ {b$ b}这看起来像这样:
begin transaction;
尝试{
保存文件的新记录;
将Word文档保存在预定义的目录中,使用记录的ID作为文件名;
} catch(Exception e){
rollback transaction;
扔e; //重新创建异常
}
commit transaction;
上面的代码假定在发生错误时引发异常。 b
I need to store and retrieve MS Word documents into MySQL 5.1 with Servlets. I've the code to upload a file, but I don't know can I feed into the table. I've used BLOB for the field I've to insert .doc files.
Here's my code snippet to upload files:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
// get access to file that is uploaded from client
Part p1 = request.getPart("file");
String type=p1.getContentType();
String name=p1.getName();
long size = p1.getSize();
InputStream is = p1.getInputStream();
//FileInputStream fis = is.
// read filename which is sent as a part
Part p2 = request.getPart("name");
Scanner s = new Scanner(p2.getInputStream());
String filename = s.nextLine(); // read filename from stream
// get filename to use on the server
String outputfile = this.getServletContext().getRealPath(filename); // get path on the server
FileOutputStream os = new FileOutputStream (outputfile);
// write bytes taken from uploaded file to target file
int ch = is.read();
while (ch != -1) {
os.write(ch);
ch = is.read();
}
os.close();
out.println("<h3>File : '" + name + "' Type : '" + type + "' "
+ "of Size : " + ((double) size/1024) + "KB uploaded successfully!</h3>");
}
catch(Exception ex) {
out.println("Exception -->" + ex.getMessage());
}
finally {
out.close();
}
}
Here, I've used Servlets 3.0 feature for uploading a file... My table schema :
resources
- UserID [varchar(15)]
- Document [mediumblob]
Could anyone help me how can I store the document into the table and though BLOB is a type representing binary data, how can I retrieve as a Word Document (*.doc)?
A partial answer on storing the Word documents in files:
You don't need any additional column to save the file name as the document's record ID can serve as the file name.
When saving a new document, do in a database transaction so that you can undo the process when something goes wrong.
In pseudo code, this would look like this:
begin transaction;
try {
save new record for document;
save Word document in predefined directory, using record's ID as the filename;
} catch (Exception e) {
rollback transaction;
throw e; // Rethrow exception
}
commit transaction;
The code above assumes that an exception is thrown when an error occurs.
这篇关于使用MySQL存储和检索word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!