首先下载一些jar文件导入你的工程
commons-io-1.4-bin.zip
commons-beanutils-1.8.0-bin.zip
commons-fileupload-1.2.1-bin.zip
可以在apache官网上下
改变新浪编辑器中的img.htm
将其中的表单中的action的地址变成你定义的处理上传的servlet的地址。
如:
此处J_ClubUnion 为我的项目名,我用的是tomcat。
在web.xml中配置在<web-app></web-app>之中添加
接着,在你的web根目录下建立 upload/images_tmp与 upload/images文件夹
接着编写在servlet包中的UploadImage
然后依葫芦画瓢,创建servlet AttachUpload,upload/attach_tmp和upload/attach 并修改attach.htm 。
AttachUpload如下
commons-io-1.4-bin.zip
commons-beanutils-1.8.0-bin.zip
commons-fileupload-1.2.1-bin.zip
可以在apache官网上下
改变新浪编辑器中的img.htm
将其中的表单中的action的地址变成你定义的处理上传的servlet的地址。
如:
<form name="form1" id="form1" method="post" action="/J_ClubUnion/servlet/ImageUpload" enctype="multipart/form-data" target="myiframe">
此处J_ClubUnion 为我的项目名,我用的是tomcat。
在web.xml中配置在<web-app></web-app>之中添加
<servlet>
<servlet-name>ImageUpload</servlet-name>
<servlet-class>servlet.ImageUpload</servlet-class>
</servlet>
…………………………
<servlet-mapping>
<servlet-name>ImageUpload</servlet-name>
<url-pattern>/servlet/ImageUpload</url-pattern>
</servlet-mapping>
接着,在你的web根目录下建立 upload/images_tmp与 upload/images文件夹
接着编写在servlet包中的UploadImage
package servlet;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.DefaultFileItemFactory;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
/**
*
* @author maosuhan
*/
public class ImageUpload extends HttpServlet implements javax.servlet.Servlet {
File tmpDir = null;//初始化上传文件的临时存放目录
File saveDir = null;//初始化上传文件后的保存目录
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
tmpDir=new File(this.getServletContext().getRealPath("/")+"upload"+File.separator+"images_tmp/");
saveDir=new File(this.getServletContext().getRealPath("/")+"upload"+File.separator+"images/");
// response.setContentType("text/script;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
if(ServletFileUpload.isMultipartContent(request)){
DiskFileItemFactory dff = new DiskFileItemFactory();//创建该对象
dff.setRepository(tmpDir);//指定上传文件的临时目录
dff.setSizeThreshold(1024000);//指定在内存中缓存数据大小,单位为byte
ServletFileUpload sfu = new ServletFileUpload(dff);//创建该对象
sfu.setFileSizeMax(5000000);//指定单个上传文件的最大尺寸
sfu.setSizeMax(10000000);//指定一次上传多个文件的总尺寸
FileItemIterator fii = sfu.getItemIterator(request);//解析request 请求,并返回FileItemIterator集合
while(fii.hasNext()){
FileItemStream fis = fii.next();//从集合中获得一个文件流
if(!fis.isFormField() && fis.getName().length()>0){//过滤掉表单中非文件域
// String fileName = fis.getName().substring(fis.getName().lastIndexOf("."));//获得上传文件的文件名
String fileName=(new Date()).toString()+(int)(Math.random()*1000)+fis.getName().substring(fis.getName().lastIndexOf("."));
fileName=fileName.replace(" ", "");
fileName=fileName.replace(":", "");
BufferedInputStream in = new BufferedInputStream(fis.openStream());//获得文件输入流
BufferedOutputStream out2 = new BufferedOutputStream(new FileOutputStream(new File(saveDir+File.separator+fileName)));//获得文件输出流
Streams.copy(in, out2, true);//开始把文件写到你指定的上传文件夹
out.print("<script>window.parent.LoadIMG('../../upload/images/"+fileName+"')</script>");//这句很关键,传递给img.htm一个script语句执行,这里的相对路径是针对你的upload/images和img.htm来讲的,可能你的路径有所不同
}
}
}
} catch(Exception e){
e.printStackTrace();
}
finally {
out.close();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
然后依葫芦画瓢,创建servlet AttachUpload,upload/attach_tmp和upload/attach 并修改attach.htm 。
AttachUpload如下
package servlet;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
public class AttachUpload extends HttpServlet {
File tmpDir,saveDir;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
tmpDir=new File(this.getServletContext().getRealPath("/")+"upload"+File.separator+"attach_tmp/");
saveDir=new File(this.getServletContext().getRealPath("/")+"upload"+File.separator+"attach/");
PrintWriter out = response.getWriter();
try {
if(ServletFileUpload.isMultipartContent(request)){
DiskFileItemFactory dff = new DiskFileItemFactory();//创建该对象
dff.setRepository(tmpDir);//指定上传文件的临时目录
dff.setSizeThreshold(1024000);//指定在内存中缓存数据大小,单位为byte
ServletFileUpload sfu = new ServletFileUpload(dff);//创建该对象
sfu.setFileSizeMax(20000000);//指定单个上传文件的最大尺寸
sfu.setSizeMax(50000000);//指定一次上传多个文件的总尺寸
FileItemIterator fii = sfu.getItemIterator(request);//解析request 请求,并返回FileItemIterator集合
while(fii.hasNext()){
FileItemStream fis = fii.next();//从集合中获得一个文件流
if(!fis.isFormField() && fis.getName().length()>0){//过滤掉表单中非文件域
// String fileName = fis.getName().substring(fis.getName().lastIndexOf("."));//获得上传文件的文件名
String fileName=(new Date()).toString()+(int)(Math.random()*1000)+fis.getName().substring(fis.getName().lastIndexOf("."));
fileName=fileName.replace(" ", "");
fileName=fileName.replace(":", "");
BufferedInputStream in = new BufferedInputStream(fis.openStream());//获得文件输入流
BufferedOutputStream out2 = new BufferedOutputStream(new FileOutputStream(new File(saveDir+File.separator+fileName)));//获得文件输出流
Streams.copy(in, out2, true);//开始把文件写到你指定的上传文件夹
out.print("<script>window.parent.LoadAttach('../../upload/attach/"+fileName+"')</script>");
}
}
}
} catch(Exception e){
e.printStackTrace();
}
finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}