package com;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import com.jspsmart.upload.*;
public class uploadfiles extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
//使用了一个第三方的组件,存放在web-inf/lib下
response.setContentType("text/html;charset=GB2312");
//由于SmartUpload的初始化方法需要pageContext,所以我们在servlet中得到他
//为了得到pageConext要首先得到JspFactory的实例
//通过JspFactory的实例的getPageContext方法得到pageConext的实例
JspFactory jf = null;
//得到JspFactory的实例
jf=JspFactory.getDefaultFactory();
/*
getPageContext(Servlet servlet,
ServletRequest request,
ServletResponse response,
java.lang.String errorPageURL,
boolean needsSession,
int buffer,
boolean autoflush)
*/
PageContext pageContext=jf.getPageContext(this,request,response,null,true,8192,true);
try
{
//实例化SmartUpload
SmartUpload mySmartUpload=new SmartUpload();
//初始化SmartUpload的实例,需要PageContext的实例
mySmartUpload.initialize(pageContext);
//设定最大上传的字节数,其实可以不进行设定,表示上传的文件没有大小限制
//mySmartUpload.setTotalMaxFileSize(10000000);
mySmartUpload.upload();
//下面是单文件上传
//上传的文件以com.jspsmart.upload.File 代表,如果文件名称重复,则进行覆盖
com.jspsmart.upload.File file=mySmartUpload.getFiles().getFile(0);
String upLoadFileName=file.getFileName();
//调用com.jspsmart.upload.File实例的saveas的方法保存文件,此时的文件名即是
//保存到服务器上的文件名
file.saveAs("/upload/"+upLoadFileName);
Request req =
Text t = .....;
t.setUpload(upLoadFileName);
t.set.....(req);
}
catch(SmartUploadException e)
{
System.out.println(e.getMessage());
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException
{
doGet(request,response);
}
}