
1.用java将word文件上传到服务器,把word里面的内容保存到数据库
使用java中的io进行读取
BufferedReader bufferedReader = null;
File file = new File("文档地址+文档名.docx");
if(!file.exists()){
System.out.println("文件不存在");
} else {
bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "读取的字符格式(UTF-8或GBK)"));
String lineText = null;
while((lineText = bufferedReader.readLine()) != null){
if (linText != null && !lineText.eq("")){
System.out.println("一次读取一行,一行内容为:" + lineText);
}
}
}
2.怎么样用JAVA 实现文件的上传下载
如果不涉及到数据库的话,用简单的IO流即可实现。上传的时候你指定好文件路径或相对路径,把上传内容写进一个生成的文件。下载的时候你去搜下要下载的文件名,把该文件的内容读出来。
如果涉及数据库的话你可以这样做,用hibernate框架的情况下,你可以定义一个实体,实体里含有要下载的文件的标题,内容,这个文件储存的路径等字段。上传的时候将文件读出来并赋值给这些字段,然后存到数据库中,并且将内容,标题等写成String的类型存储到数据库中,下载的时候你可以去数据库中搜这个路径,存在即可把数据库里的内容,标题等读到本地的盘上并生成个文件即可。
3.java如何实现文件上传
public static int transFile(InputStream in, OutputStream out, int fileSize) {int receiveLen = 0;final int bufSize = 1000;try {byte[] buf = new byte[bufSize];int len = 0;while(fileSize - receiveLen > bufSize){len = in.read(buf);out.write(buf, 0, len);out.flush();receiveLen += len;System.out.println(len);}while(receiveLen < filesize){len="in.read(buf," 0,="" filesize="" -="" receivelen);system.out.println(len);out.write(buf,="" 0,="" len);receivelen="" +="len;out.flush();}}" catch="" (ioexception="" e)="" {//="" todo="" 自动生成="" catch="" 块e.printstacktrace();}return="">
那么发送文件方,InputStream就是FileInputStream,OutputStream就是Socket.getOutputStream.接受文件方,InputStream就是Socket.getInputStream,OutputStream就是FileOutputStream。就OK了。
至于存到数据库里嘛,Oracle里用Blob。搜索一下,也是一样的。
从Blob能获取一个输出流。
4.紧急求助如何用java实现word文档的导入,请大家踊跃发言,谢谢大家
环境支持 1.1 添加poi支持:包下载地址.ray.poi.util;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import org.apache.poi.poifs.filesystem.DirectoryEntry;import org.apache.poi.poifs.filesystem.DocumentEntry;import org.apache.poi.poifs.filesystem.POIFSFileSystem;import org.textmining.text.extraction.WordExtractor;/** * 读写doc * @author wangzonghao * */public class POIWordUtil { /** * 读入doc * @param doc * @return * @throws Exception */ public static String readDoc(String doc) throws Exception { // 创建输入流读取DOC文件 FileInputStream in = new FileInputStream(new File(doc)); WordExtractor extractor = null; String text = null; // 创建WordExtractor extractor = new WordExtractor(); // 对DOC文件进行提取 text = extractor.extractText(in); return text; } /** * 写出doc * @param path * @param content * @return */ public static boolean writeDoc(String path, String content) { boolean w = false; try { // byte b[] = content.getBytes("ISO-8859-1"); byte b[] = content.getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream(b); POIFSFileSystem fs = new POIFSFileSystem(); DirectoryEntry directory = fs.getRoot(); DocumentEntry de = directory.createDocument("WordDocument", bais); FileOutputStream ostream = new FileOutputStream(path); fs.writeFilesystem(ostream); bais.close(); ostream.close(); } catch (IOException e) { e.printStackTrace(); } return w; } }测试package com.ray.poi.util;import junit.framework.TestCase;public class POIUtilTest extends TestCase { public void testReadDoc() { try{ String text = POIWordUtil.readDoc("E:/work_space/poi/com/ray/poi/util/demo.doc"); System.out.println(text); }catch(Exception e){ e.printStackTrace(); } } public void testWriteDoc() { String wr; try { wr = POIWordUtil.readDoc("E:/work_space/poi/com/ray/poi/util/demo.doc"); boolean b = POIWordUtil.writeDoc("c:\\demo.doc",wr); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}。
5.java上传的文档如何在页面上显示出来
置了网页上的word文件在浏览器中打开
(工具->;选项->;常规->web选项->;浏览器)里面设置。
或者服务器端写代码:
2,
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setCharacterEncoding("GBK");
res.setContentType("application/msword");
File f = new File(xxxx);
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(f));
BufferedOutputStream bos = new BufferedOutputStream(res
.getOutputStream());
byte buff[] = new byte[4096];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length)))
bos.write(buff, 0, bytesRead);
if (bis != null)
bis.close();
if (bos != null) {
bos.flush();
bos.close();
}}
转载请注明出处51数据库 » javaword文件上传
恺撒(教主)