Demo:
1.表单页面
sssformdata.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<head>
<title>fbysss UploadBean 示例</title>
<!--meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"-->
<!--meta http-equiv="Content-Type" content="text/html; charset=gb2312"-->
</head>
<FORM name="form1" METHOD="POST" ACTION="sssupload.jsp" ENCTYPE="multipart/form-data">
<input name="title" type= "text" value="中文字">
<td class="bodystyle">附件</td>
<td class="bodystyle"> <input name="attach" type="FILE" id="attach" size="50" > </td>
<input name="ok" type= "submit" value="提交">
</form>
2.读取表单页面
sssgetdata.jsp
<!--
//==========================================================================
//文件:UploadBean上传实例
//功能:解决中文乱码,完成文件上传,并提供上传改名解决方案
//作者:fbysss
//msn:jameslastchina@hotmail.com
//==========================================================================
-->
<%@ 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"%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<%
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("<br>Title0是:"+sTt0+"<br>");
String sTt1 = new String(sTt0.getBytes("ISO-8859-1"),"GBK");
out.println("<br>Title1是:"+sTt1+"<br>");
//这里用来测试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
分享到:
相关推荐
JSPSmartUpload上传文件乱码解决纪实+UploadBean上传解决方案
JSP SmartUpload上传文件乱码解决纪实+UploadBean上传解决方案.mht 和代码
用smartupload一直很不尽如人意,尤其是中文方面的支持,现在好了,uploadbean还可以,同时还有和他搭配的jar一并打包~
UploadBean详解 UploadBean详解
支持多文件上传 需要数据库支持(可以修改不支持)。 没有文档。
此组件用于文件上传,解决了中文文件上传乱码问题
jsp uploadbean用于页面上传
cos.jar + uploadbean.jar + filemover.jar 方法实现jsp上传文件的 javaWeb程序。。内含cos.jar 、 uploadbean.jar 、 filemover.jar 在eclipse上皆可运行。
主要通过uploadbean和filemover上传文件和下载文件
uploadbean1.7uploadbean1.7uploadbean1.7
uploadbean1.5.zip 文件上传下载包 ,轻松搞定文件上传下载
ajax + jquery + uploadbean
ajax + jquery + uploadbean的使用
用于图片上传功能,前台后台都能实现 后台实现代码: FileMover fileMover = new FileMover(); UploadBean upBean = new UploadBean(); MultipartFormDataRequest mrequest = null; Hashtable files = null; ...
表单递交上传文件先存数据库再并以邮件附件发出 uploadbean MultipartFormDataRequest smtp sql
自己写的文件上传组建,不占用内存,速度快。 uploadbean 组建 一定要按照页面上的... 文件在 classes.servlet.UploadBean 文件 <br>classes.servlet.MyServlet 为调用文件 index.jsp 为上传界面。
public UploadBean() { } //设置上传文件的后缀名 public void setSuffix(String canSuffix) { this.canSuffix = canSuffix; } //设置文件保存路径 public void setObjectPath(String objectPath) { this....
由asp版本修改。补充:内容的存取,特殊字符(如引号)最好replace转义...上传bean是src\com\UploadBean.java 上传处理在\Edit\editor\upload.jsp 部分浏览器预览本地图片小红叉可改路径调试。如request.getRealPath("/")