response.contenttype,java如何实现word转PDF

伏羲号

response.contenttype,java如何实现word转PDF?

openoffice+jodconverter的转换方案,可以用纯的java代码完成转换工作,服务器端需要

安装

response.contenttype,java如何实现word转PDF

openoffice,但是需求一步额外的操作--需要在服务器上的某个端口提供一个openoffice服务,这对部署起来显得麻烦了点。google code上有一个jodconverter 3,此版完全把2给重构了,它可以帮你创建oo服务监听在指定端口。 .jodconverter 3.0 requires:

java 1.5 or later openoffice.org 3.0.0 or later

package org.artofsolving.jodconverter.sample.web; import java.io.file; import java.util.logging.logger; import javax.

servlet开发

.

servlet开发

context; import org.apache.commons.fileupload.disk.diskfileitemfactory; import org.apache.commons.fileupload.

servlet开发

.

servlet开发

fileupload; import org.artofsolving.jodconverter.officedocumentconverter; import org.artofsolving.jodconverter.office.managedprocessofficemanager; import org.artofsolving.jodconverter.office.managedprocessofficemanagerconfiguration; import org.artofsolving.jodconverter.office.officeconnectionmode; import org.artofsolving.jodconverter.office.officemanager; public class webappcontext { public static final string parameter_office_port = "office.port"; public static final string parameter_office_home = "office.home"; public static final string parameter_office_profile = "office.profile"; public static final string parameter_fileupload_file_size_max = "fileupload.filesizemax"; private final logger logger = logger.getlogger(getclass().getname()); private static final string key = webappcontext.class.getname(); private final

servlet开发

fileupload fileupload; private final officemanager officemanager; private final officedocumentconverter documentconverter; public webappcontext(

servlet开发

context

servlet开发

context) { diskfileitemfactory fileitemfactory = new diskfileitemfactory(); string filesizemax =

servlet开发

context.getinitparameter(parameter_fileupload_file_size_max); fileupload = new

servlet开发

fileupload(fileitemfactory); if (filesizemax != null) { fileupload.setfilesizemax(integer.parseint(filesizemax)); logger.info("max file upload size set to " + filesizemax); } else { logger.warning("max file upload size not set"); } int officeport = 8100; string officeportparam =

servlet开发

context.getinitparameter(parameter_office_port); if (officeportparam != null) { officeport = integer.parseint(officeportparam); } officeconnectionmode connectionmode = officeconnectionmode.socket(officeport); managedprocessofficemanagerconfiguration configuration = new managedprocessofficemanagerconfiguration(connectionmode); string officehomeparam =

servlet开发

context.getinitparameter(parameter_office_home); if (officehomeparam != null) { configuration.setofficehome(new file(officehomeparam)); } string officeprofileparam =

servlet开发

context.getinitparameter(parameter_office_profile); if (officeprofileparam != null) { configuration.settemplateprofiledir(new file(officeprofileparam)); } officemanager = new managedprocessofficemanager(configuration); documentconverter = new officedocumentconverter(officemanager); } protected static void init(

servlet开发

context

servlet开发

context) { webappcontext instance = new webappcontext(

servlet开发

context);

servlet开发

context.setattribute(key, instance); instance.officemanager.start(); } protected static void destroy(

servlet开发

context

servlet开发

context) { webappcontext instance = get(

servlet开发

context); instance.officemanager.stop(); } public static webappcontext get(

servlet开发

context

servlet开发

context) { return (webappcontext)

servlet开发

context.getattribute(key); } public

servlet开发

fileupload getfileupload() { return fileupload; } public officemanager getofficemanager() { return officemanager; } public officedocumentconverter getdocumentconverter() { return documentconverter; } }

在web应用启动时初始化:

package org.artofsolving.jodconverter.sample.web; import javax.

servlet开发

.

servlet开发

contextevent; import javax.

servlet开发

.

servlet开发

contextlistener; public class webappcontextlistener implements

servlet开发

contextlistener { public void contextinitialized(

servlet开发

contextevent event) { webappcontext.init(event.get

servlet开发

context()); } public void contextdestroyed(

servlet开发

contextevent event) { webappcontext.destroy(event.get

servlet开发

context()); } }

转换

servlet开发

package org.artofsolving.jodconverter.sample.web; import java.io.file; import java.io.fileinputstream; import java.io.ioexception; import java.io.inputstream; import java.util.list; import java.util.logging.logger; import javax.

servlet开发

.

servlet开发

exception; import javax.

servlet开发

.http.http

servlet开发

; import javax.

servlet开发

.http.http

servlet开发

request; import javax.

servlet开发

.http.http

servlet开发

response; import org.apache.commons.fileupload.fileitem; import org.apache.commons.fileupload.fileuploadexception; import org.apache.commons.fileupload.

servlet开发

.

servlet开发

fileupload; import org.apache.commons.io.filenameutils; import org.apache.commons.io.ioutils; import org.artofsolving.jodconverter.documentformat; import org.artofsolving.jodconverter.officedocumentconverter; public class converter

servlet开发

extends http

servlet开发

{ private static final long serialversionuid = -591469426224201748l; private final logger logger = logger.getlogger(getclass().getname()); @override protected void dopost(http

servlet开发

request request, http

servlet开发

response response) throws

servlet开发

exception, ioexception { if (!

servlet开发

fileupload.ismultipartcontent(request)) { response.senderror(http

servlet开发

response.sc_forbidden, "only multipart requests are allowed"); return; } webappcontext webappcontext = webappcontext.get(get

servlet开发

context());

servlet开发

fileupload fileupload = webappcontext.getfileupload(); officedocumentconverter converter = webappcontext .getdocumentconverter(); string outputextension = filenameutils.getextension(request .getrequesturi()); fileitem uploadedfile; try { uploadedfile = getuploadedfile(fileupload, request); } catch (fileuploadexception fileuploadexception) { throw new

servlet开发

exception(fileuploadexception); } if (uploadedfile == null) { throw new nullpointerexception("uploaded file is null"); } string inputextension = filenameutils.getextension(uploadedfile .getname()); string basename = filenameutils.getbasename(uploadedfile.getname()); file inputfile = file.createtempfile(basename, "." + inputextension); writeuploadedfile(uploadedfile, inputfile); file outputfile = file.createtempfile(basename, "." + outputextension); try { documentformat outputformat = converter.getformatre

gis地理信息系统

try() .getformatbyextension(outputextension); long starttime = system.currenttimemillis(); converter.convert(inputfile, outputfile); long conversiontime = system.currenttimemillis() - starttime; logger.info(string.format( "successful conversion: %s [%db] to %s in %dms", inputextension, inputfile.length(), outputextension, conversiontime)); response.setcontenttype(outputformat.getmediatype()); response.setheader("content-disposition", "attachment; filename=" + basename + "." + outputextension); sendfile(outputfile, response); } catch (exception exception) { logger.severe(string.format( "failed conversion: %s [%db] to %s; %s; input file: %s", inputextension, inputfile.length(), outputextension, exception, inputfile.getname())); throw new

servlet开发

exception("conversion failed", exception); } finally { outputfile.delete(); inputfile.delete(); } } private void sendfile(file file, http

servlet开发

response response) throws ioexception { response.setcontentlength((int) file.length()); inputstream inputstream = null; try { inputstream = new fileinputstream(file); ioutils.copy(inputstream, response.getoutputstream()); } finally { ioutils.closequietly(inputstream); } } private void writeuploadedfile(fileitem uploadedfile, file destinationfile) throws

servlet开发

exception { try { uploadedfile.write(destinationfile); } catch (exception exception) { throw new

servlet开发

exception("error writing uploaded file", exception); } uploadedfile.delete(); } private fileitem getuploadedfile(

servlet开发

fileupload fileupload, http

servlet开发

request request) throws fileuploadexception { @suppresswarnings("unchecked") list<fileitem> fileitems = fileupload.parserequest(request); for (fileitem fileitem : fileitems) { if (!fileitem.isformfield()) { return fileitem; } } return null; } }

浏览器文件保存编码是什么?

我们常用的系统文件编码有;ANST、Unicode、Unicode big endian、utf-8 四种。编码不一样,存储方式不一样 。 不同的编码只能显示特定的内容,一般在复制保存网页文件是会遇到这种情况。

一、火狐下载中文文件名乱码

导致乱码的写法:

response.addHeader(“Content-Disposition”, “attachment;filename=中文文件名.xml”,”utf-8”));

修正后的写法

response.addHeader(“Content-Disposition”, “attachment;filename*=utf-8’zh_cn’中文文件名.xml”,”utf-8”));

不过第二种写法并不兼容ie8,下载后的文件名为:写了该代码的JSP页面的名称,在IE11上测试没有问题。

二、下载文件默认编码修改

Java代码

String resultStr=”返回结果”;

byte[] bytes = resultStr.getBytes(“utf-8”); //字符串转Byte数组,加编码格式。

response的contentType几种类型?

这个方法设置发送到客户端的响应的内容类型,此时响应还没有提交。给出的内容类型可以包括字符编码说明,例如:text/html;charset=utf-8.如果该方法在getwriter()方法被调用之前调用,那么响应的字符编码将仅从给出的内容类型中设置。该方法如果在getwriter()方法被调用之后或者在被提交之后调用,将不会设置响应的字符编码,在使用http协议的情况中,该方法设置content-type实体报头。

一般在servlet中,习惯性的会首先设置请求以及响应的内容类型以及编码方式:

response.setcontentType("text/html;charset=utf-8");

request.setcharacterencoding("utf-8");

servlet怎么返回一个json对象?

response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); JSONObject json=new JSONObject();//json给值省略response.getWriter().println(json.toString());

如何把ACCESS中的数据按格式导出到word中?

你把ACCESS输出到网页没有问题吧?那就在文件头加上:Response.Buffer = Trueresponse.ContentType ="Application/msword"这样就生成WORD了。

发表评论

快捷回复: 表情:
AddoilApplauseBadlaughBombCoffeeFabulousFacepalmFecesFrownHeyhaInsidiousKeepFightingNoProbPigHeadShockedSinistersmileSlapSocialSweatTolaughWatermelonWittyWowYeahYellowdog
评论列表 (暂无评论,112人围观)

还没有评论,来说两句吧...