UploadBean实现文件上传

阅读更多
UploadBean上传解决方案
满意指数:五星
完成程度:100%
Demo:
1.表单页面sssformdata.jsp
<%@ page contentType="text/html;charset=gb2312" %>

fbysss UploadBean 示例





附件



2.读取表单页面sssgetdata.jsp

<%@ page contentType="text/html;charset=GBK" %>
<%@ page language="java" import="com.jspsmart.upload.*"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="java.io.File"%>
<%@ page import="java.util.*"%>
<%@ page import="javazoom.upload.*"%>
<%@ page import="uploadutilities.FileMover"%>



<%
request.setCharacterEncoding("GBK");//设置编码格式,就不用一个个转码了。
FileMover fileMover = new FileMover();//你也可以使用自带的实例中jsp:useBean的形式。
UploadBean upBean = new UploadBean();
MultipartFormDataRequest mrequest = null;
Hashtable files = null;

if (MultipartFormDataRequest.isMultipartFormData(request))
{

mrequest = new MultipartFormDataRequest(request,null,100*1024*1024,MultipartFormDataRequest.COSPARSER,"GBK");//注意这里也要设置编码参数
String sTt0 = mrequest.getParameter("title");
out.println("
Title0是:"+sTt0+"
");
String sTt1 = new String(sTt0.getBytes("ISO-8859-1"),"GBK");
out.println("
Title1是:"+sTt1+"
");
//这里用来测试title参数是否正确。调试的时候,加一句if (true)return;即可。
files = mrequest.getFiles();
}


//获取修改前的文件名
String sOldFileName =mrequest.getParameter("oldfilename");
out.println("sOldFileName:"+sOldFileName);
String sWebRootPath = request.getRealPath("/");//得到你的web应用的根。
String sPath=sWebRootPath+"attach";
int iFileCount = 0;
String sServerFileName="";
String sLocalFileName = "";
//文件获取
if ( (files != null) || (!files.isEmpty()) ) {

iFileCount = files.size();
UploadFile file = (UploadFile) files.get("attach");
sLocalFileName=file.getFileName();
out.println("sLocalFileName:"+sLocalFileName);
int ii= sLocalFileName.indexOf("."); //取文件名的后缀
String sExt = sLocalFileName.substring(ii,sLocalFileName.length());
//得到不重复的文件名
java.util.Date dt = new java.util.Date(System.currentTimeMillis());
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
sServerFileName= fmt.format(dt);
sServerFileName =sServerFileName + sExt;
//如果不存在该目录,则新建一个
File dir =new File(sPath);
if (!dir.exists()){
dir.mkdirs();
}
upBean.setFolderstore(sPath);//设置要上传的目录
upBean.addUploadListener(fileMover);//增加filMover监听
fileMover.setNewfilename(sServerFileName);//设置服务器上的文件名
upBean.store(mrequest, "attach");//上传
out.println("file path is "+sPath+"/"+sServerFileName);
}

%>

Demo注意事项:cos.jar,uploadbean.jar,filemover.jar这几个包必须有。
必备下载地址:
UploadBean1.5:http://wcarchive.cdrom.com/pub/simtelnet/winnt/java/uploadbean1_5.zip
FileMover1.3:http://www.javazoom.net/jzservlets/uploadbean/addons/filemover1.3.zip
参考下载地址:
ChinesUpload例子:http://www.javazoom.net/jzservlets/uploadbean/addons/ChineseUpload.zip

你可能感兴趣的:(Java,JSP,.net,应用服务器,HTML)