用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);}}}
用java实现文件的上传与下载
1.下载简单,无非是把服务器上的文件或者数据库中的BLob(或其他二进制型),用流读出来,然后写到客户端即可,要注意 ContentType。
2.上传,可以用Apache Commons Upload等开源工具,或者自己写:form要用enctype="multipart/form-data"然后服务器端也是用IO把客户端提交的文件流读入,然后写到服务器的文件系统或者数据库里。
不同的数据库对Lob字段操作可能有所不同,建议用Hibernate,JPA等成熟的ORM框架,可以不考虑数据库细节。
Java程序把Word文档直接换成Html文件方法是什么呢?
acob是Java和Windows下的Com桥,通过它我们可以在Java程序中调用COM组件。
如果你的JDK是1.4,那你需要下载Jacob 1.9的jni库才能正常运行,早期版本在JDK 1.4下有些问题。
以下是引用片段:package com;/*** 〈p〉Title:Word文档转html类〈/p〉* 〈p〉Description: 〈/p〉* 〈p〉Copyright:() 2002〈/p〉* @author 舵手* @version 1.0*/import com.jacob.com.*;import com.jacob.activeX.*;public class WordtoHtml {/***文档转换函数*@param docfile word文档的绝对路径加文件名(包含扩展名)*@param htmlfile 转换后的html文件绝对路径和文件名(不含扩展名)*/public static void change(String docfile, String htmlfile) {ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动wordtry {app.setProperty("Visible", new Variant(false));//设置word不可见Object docs = app.getProperty("Documents").toDispatch();Object doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[]{ docfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch();// 打开word文件Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {htmlfile,new Variant (8) }, new int[1]);// 作为html格式保存到临时文件Variant f = new Variant(false);Dispatch.call(doc, "Close", f);} catch (Exception e) {e.printStackTrace();} finally {app.invoke("Quit", new Variant[]{});}}public static void main(String[] strs){WordtoHtml.change("c:\\a\\运输管理调度系统总体方案.doc", "c:\\a\\t");}}
java web 文件上传怎么修改上传到本地的目录
byte response.setContentType("//得到向客户端输出二进制数据的对象&nbsp.getWriter();hFile;copy@RequestMapping("/getimgs")&nbsp.write(&quot.close().setContentType("text/html;charset=utf-8"); toClient; toClient=response; toClient data[]=new getimg(String saveAddress //错误处理 public { PrintWriter&nbsp.read(data); //读数据&nbsp:[java] &nbsp.close();无法打开图片!"); toClient;// toClient.write(data); //输出数据 toClient;new FileInputStream(saveAddress);hFile = &nbsp,src=&quot.close(); &nbsp.available(); 请求后台路径" 以byte流的方式打开文件 d:\1;byte[i];} catch(IOException //得到向客户端输出文本的对象 response;= response;image/*"); //设置返回的文件类型 OutputStream ,HttpServletRequest request, HttpServletResponse ,后台以输出流的方式返回文件即可后台代码; hFile.使用请求的方式; i=hFile; //得到文件大小 e) void response) throws IOException{ try{ FileInputStream view plain &nbsp.getOutputStream();&nbsp.gif int } } &nbsp1
JAVA中如何把WORD文档直接转换成html?
jaco是java和windows下的com桥,通过它我们可以在java程序中调用COM组件。
如果你的JDK是1.4,那你需要下载jaco1.9的jni库才能正常运行,早期版本在JDK1.4下有些问题。
package com;** * Title:Wod文档转html类 * Desciption: * Copyight:() 2002 * @autho 舵手 * @vesion 1.0 *impot com.jaco.com.*;impot com.jaco.activeX.*; pulic class WodtoHtml { ** *文档转换函数 *@paam docfile wod文档的绝对路径加文件名(包含扩展名) *@paam htmlfile 转换后的html文件绝对路径和文件名(不含扩展名) * pulic static void change(Sting docfile, Sting htmlfile) { ActiveXComponent app = new ActiveXComponent("Wod.Application"); 启动wod ty { app.setPopety("Visile", new Vaiant(false)); 设置wod不可见 Oject docs = app.getPopety("Documents").toDispatch(); Oject doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Oject[] { docfile, new Vaiant(false),new Vaiant(tue) }, new int[1]).toDispatch(); 打开wod文件 Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Oject[] {htmlfile, new Vaiant(8) }, new int[1]); 作为html格式保存到临时文件 Vaiant f = new Vaiant(false); Dispatch.call(doc, "Close", f); } catch (Exception e) { e.pintStackTace(); } finally { app.invoke("Quit", new Vaiant[]{}); } } pulic static void main(Sting[] sts){ WodtoHtml.change("c:\\a\\运输管理调度系统总体方案.doc", "c:\\a\\t"); }}
Java如何操作Word,Excel,PDF文档?
Java Excel API 文档 http://www.andykhan.com/jexcelapi/ 1、一个jacob操作Word的例子,其他操作excel,pdf的sample里都有 import java.io.File; import com.jacob.com.*; import com.jacob.activeX.*; public class WordTest { public static void main(String[] args) { WordBean word=new WordBean(); word.openWord(true); word.createNewDocument(); word.insertText("Hello word."); } } import com.jacob.activeX.*; import com.jacob.com.*; public class WordBean extends java.awt.Panel { private ActiveXComponent MsWordApp = null; private Dispatch document = null; public WordBean() { super(); } public void openWord(boolean makeVisible) { //Open Word if we've not done it already if (MsWordApp == null) { MsWordApp = new ActiveXComponent("Word.Application"); } //Set the visible property as required. Dispatch.put(MsWordApp, "Visible", new Variant(makeVisible)); } public void createNewDocument() { //Find the Documents collection object maintained by Word Dispatch documents = Dispatch.get(MsWordApp,"Documents").toDispatch(); //Call the Add method of the Documents collection to create //a new document to edit document = Dispatch.call(documents,"Add").toDispatch(); } public void insertText(String textToInsert) { // Get the current selection within Word at the moment. If // a new document has just been created then this will be at // the top of the new doc Dispatch selection = Dispatch.get(MsWordApp,"Selection").toDispatch(); //Put the specified text at the insertion point Dispatch.put(selection,"Text",textToInsert); } public void saveFileAs(String filename) { Dispatch.call(document,"SaveAs",filename); } public void printFile() { //Just print the current document to the default printer Dispatch.call(document,"PrintOut"); } public void closeDocument() { // Close the document without saving changes // 0 = wdDoNotSaveChanges // -1 = wdSaveChanges // -2 = wdPromptToSaveChanges Dispatch.call(document, "Close", new Variant(0)); document = null; } public void closeWord() { Dispatch.call(MsWordApp,"Quit"); MsWordApp = null; document = null; } }
java web 如何获得文件上传大小
有一种叫jspsmartupload的包用来简化文件上传下载的编写里面可以获取文件大小//取得文件com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);//取得文件名String fileName = myFile.getFileName();//取得文件大小int fileSize = myFile.getSize();这是基于spring架构的上传文件支持多个文件上传,拿到file对象后,直接file.size()就可以获取文件的大小,if (request instanceof MultipartHttpServletRequest) {MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;for (Iterator it = multipartHttpServletRequest.getFileNames(); it.hasNext();) {String key = (String) it.next();MultipartFile file = multipartHttpServletRequest.getFile(key);String originalFilename = file.getOriginalFilename();long size = file.getSize();//文件大小需要转换成KB或Mif (StringUtils.isNotBlank(originalFilename)) {String suffixName = originalFilename.indexOf(".") == -1 ? "" : originalFilename.substring(originalFilename.lastIndexOf(".") + 1).toLowerCase();try {InputStream inputStream = file.getInputStream();byte[] copyToByteArray = FileCopyUtils.copyToByteArray(inputStream);} catch (IOException e) {e.printStackTrace();}}}}
java中关于上传文件,修改上传文件信息时怎么让之前上传的文件在上...
我说一下一般的步骤吧:1. 上传文件。
2. 将文件路径、文件名称等信息存入数据库或者session缓存中3. 再次上传的时候读取上传记录,将已上传的文件信息列出,并增加删除和修改选项。
4. 如果进行删除操作,根据保存的记录获取需要删除的文件路径,删除之。
...
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数据库 » java word文件上传