我制作了ckeditor插件来将doc文件转换为html,该文件通过带有ajax的servlet上传并转换为html。但我没有从 servlet 获得所需的 html 响应。结果我得到了相同的jsp页面。
这是Servlet代码。
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// HttpSession
response.setContentType("text/html"); // session;
if (!ServletFileUpload.isMultipartContent(request)) {
response.sendError(HttpServletResponse.SC_FORBIDDEN,
"only multipart requests are allowed");
return;
}
WebappContext webappContext = WebappContext.get(getServletContext());
ServletFileUpload fileUpload = webappContext.getFileUpload();
OfficeDocumentConverter converter = webappContext
.getDocumentConverter();
String outputExtension = FilenameUtils.getExtension(request
.getRequestURI());
FileItem uploadedFile;
try {
uploadedFile = getUploadedFile(fileUpload, request);
// FileHandler.checkForValidExtn(uploadedFile);
} catch (FileUploadException fileUploadException) {
throw new ServletException(fileUploadException);
}
if (uploadedFile == null) {
throw new NullPointerException("uploaded file is null");
}
String inputExtension = FilenameUtils.getExtension(uploadedFile
.getName());
String baseName = FilenameUtils.getBaseName(uploadedFile.getName());
// File fileFolder=new File(getServletContext().getRealPath("/")
// +"images");
// fileFolder.mkdir();
// File inputFile=new File(fileFolder+"\\"+ baseName + "." +
// inputExtension);
File inputFile = new File(getServletContext().getRealPath("/")
+ baseName + "." + inputExtension);
writeUploadedFile(uploadedFile, inputFile);
File outputFile = new File(getServletContext().getRealPath("/")
+ baseName + "." + "html");
// File outputFile=new File(fileFolder+"\\"+ baseName + "." + "html");
// outputFile.mkdirs();
System.out.println("input file path:" + inputFile.getAbsolutePath());
System.out.println("outPut file path:" + outputFile.getAbsolutePath());
try {
// DocumentFormat outputFormat = converter.getFormatRegistry()
// .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));
} 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 ServletException("conversion failed", exception);
} finally {
inputFile.delete();
}
PrintWriter out = response.getWriter();
out.println("<b>File Sent to server</b>");
out.close();
}
这是用于将 sendi 文件发送到服务器的代码 javascript 代码。
CKEDITOR.dialog.add( 'uploadDoc', function( editor )
{ var dialougeDefination =
{ title: editor.lang.uploadDoc.labelName,
resizable: CKEDITOR.DIALOG_RESIZE_BOTH,
minWidth: 250,
minHeight: 50,
onOk: function()
{ if(document.getElementById("flFileBrowser").value == "")
{ alert("No file selected");
//return;
}
else
{
$.ajax({
beforeSend: function(){
{ $("#frmFileUpload").ajaxSubmit();
$.blockUI();
}
},
success: function(result)
{ alert(result);
$.unblockUI();
}
});