这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用
后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下)
<%&#64; page language&#61;"java" import&#61;"java.util.*" pageEncoding&#61;"utf-8"%><%&#64;
page contentType&#61;"text/html;charset&#61;utf-8"%><%&#64;
page import &#61; "Xproer.*" %><%&#64;
page import&#61;"org.apache.commons.lang.StringUtils" %><%&#64;
page import&#61;"org.apache.commons.fileupload.*" %><%&#64;
page import&#61;"org.apache.commons.fileupload.disk.*" %><%&#64;
page import&#61;"org.apache.commons.fileupload.servlet.*" %><%
boolean isMultipart &#61; ServletFileUpload.isMultipartContent(request);
FileItemFactory factory &#61; new DiskFileItemFactory();
ServletFileUpload upload &#61; new ServletFileUpload(factory);
List files &#61; null;
try
{
files &#61; upload.parseRequest(request);
}
catch (FileUploadException e)
{
out.println("上传文件异常&#xff1a;"&#43;e.toString());
return;
}
FileItem imgFile &#61; null;
Iterator fileItr &#61; files.iterator();
while (fileItr.hasNext())
{
imgFile &#61; (FileItem) fileItr.next();
if(imgFile.isFormField())
{
String fn &#61; imgFile.getFieldName();
String fv &#61; imgFile.getString();
if(fn.equals("uname")) uname &#61; fv;
if(fn.equals("upass")) upass &#61; fv;
}
else
{
break;
}
}
Uploader up &#61; new Uploader(pageContext,request);
up.SaveFile(imgFile);
String url &#61; up.GetFilePathRel();
out.write(url);
response.setHeader("Content-Length",url.length()&#43;"");
%>
配置web.xml
xmlns&#61;"http://java.sun.com/xml/ns/javaee"
xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation&#61;"http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
index.jsp
前端&#xff08;页面&#xff09;测试代码&#xff1a;
实现后的效果&#xff0c;能够批量上传word中的全部图片&#xff0c;大幅度提升图片上传效率。而且能够保留Word样式。
图片上传完后&#xff0c;编辑器里面的图片地址也全部都改成了服务器的图片地址了&#xff0c;
所有代码已经全部传到网上&#xff0c;有需要的朋友可以直接下载使用&#xff0c;http://blog.ncmem.com/wordpress/2019/08/07/javaword图片上传控件/
讨论群&#xff1a;223813913