首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 图形图象-> Authorware教程
用JSP实现上传文件的两种方法-JSP教程,Jsp/Servlet
作者:网友供稿 点击:69
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
在用java开发企业器系统的使用,特别是涉及到与办公相关的软件开发的时候,文件的上传是客户经常要提到的要求.因此有 一套很好文件上传的解决办法也能方便大家在这一块的开发.

首先申明,该文章是为了自己记录一备以后开发需要的时候,不用手忙脚乱哈哈........

现在在国内用的非常多的一般是两种方法解决来解决文件上传.

cos.jar + uploadbean.jar + filemover.jar
这个是用的非常普遍的,原因是因为他操作方便,是我们不必再去关注,那些文件的输入和输出流,使我们从底层的流中解脱出来.
uploadfile,uploadbean,multipartformdatarequest
<%@ 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
参考了fbysss的一篇文章
2. org.apache.struts.upload.formfile
struts1.1的org.apache.struts.upload.formfile类。很方便,不用自己写。也不用写一个jsp调用jspsmartupload就可以搞定。


选择上传文件页面:selfile.jsp


--------------------------------------------------------------------------------
<%@ taglib uri="/web-inf/struts-html.tld" prefix="html"%>
<html:html>
<html:form action="/uploadsaction.do" enctype="multipart/form-data">
<html:file property="thefile"/>
<html:submit/>
</html:form>
</html:html>

--------------------------------------------------------------------------------
uploadaction.java
--------------------------------------------------------------------------------
import java.io.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.upload.formfile;

/**
* <p>title:uploadaction</p>
* <p>description: qrrsmms </p>
* <p>copyright: copyright (c) 2004 jiahansoft</p>
* <p>company: jiahansoft</p>
* @author wanghw
* @version 1.0
*/

public class uploadaction extends action {
public actionforward execute(actionmapping mapping,
actionform form,
httpservletrequest request,
httpservletresponse response)
throws exception {
if (form instanceof uploadsform) {//如果form是uploadsform
string encoding = request.getcharacterencoding();
if ((encoding != null) && (encoding.equalsignorecase("utf-8")))
{
response.setcontenttype("text/html; charset=gb2312");//如果没有指定编码,编码格式为gb2312
}
uploadform theform = (uploadform ) form;
formfile file = theform.getthefile();//取得上传的文件
try {
inputstream stream = file.getinputstream();//把文件读入
string filepath = request.getrealpath("/");//取当前系统路径
bytearrayoutputstream baos = new bytearrayoutputstream();
outputstream bos = new fileoutputstream(filepath + "/" +
file.getfilename());//建立一个上传文件的输出流
//system.out.println(filepath+"/"+file.getfilename());
int bytesread = 0;
byte[] buffer = new byte[8192];
while ( (bytesread = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesread);//将文件写入服务器
}
bos.close();
stream.close();
}catch(exception e){
system.err.print(e);
}
//request.setattribute("dat",file.getfilename());
return mapping.findforward("display");
}
return null;
}
}


--------------------------------------------------------------------------------

uploadform.java

--------------------------------------------------------------------------------


import javax.servlet.http.httpservletrequest;
import org.apache.struts.action.*;
import org.apache.struts.upload.*;

/**
* <p>title:uploadform</p>
* <p>description: qrrsmms </p>
* <p>copyright: copyright (c) 2004 jiahansoft</p>
* <p>company: jiahansoft</p>
* @author wanghw
* @version 1.0
*/

public class uploadform extends actionform {
public static final string error_property_max_length_exceeded = "org.apache.struts.webapp.upload.maxlengthexceeded";
protected formfile thefile;
public formfile getthefile() {
return thefile;
}
public void setthefile(formfile thefile) {
this.thefile = thefile;
}
public actionerrors validate(actionmapping mapping, httpservletrequest request)
{
actionerrors errors = null;
//has the maximum length been exceeded?
boolean maxlengthexceeded = (boolean)
request.getattribute(multipartrequesthandler.attribute_max_length_exceeded);
if ((maxlengthexceeded != null) && (maxlengthexceeded.booleanvalue()))
{
errors = new actionerrors();
errors.add(error_property_max_length_exceeded, new actionerror("maxlengthexceeded"));
}
return errors;

}
}
//这是相对应的form,还有其他属性可以设置,具体可以参考struts的上传例子。



--------------------------------------------------------------------------------

struts-config.xml

--------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<!doctype struts-config public "-//apache software foundation//dtd struts configuration 1.1//en" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="uploadsform" type="uploadform" />
</form-beans>
<action-mappings>
<action name="uploadsform" type="uploadaction" path="/uploadsaction">
<forward name="display" path="/display.jsp" />
</action>
</action-mappings>
</struts-config>
<!--display.jsp就是随便写一个成功页-->


文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·Java面试中的陷阱-JSP教程,Java技巧及代码
·通过JavaMail访问Hotmail邮箱-JSP教程,Java技巧及代码
·ASP.NET中使用自定义验证控件-.NET教程,Asp.Net开发
·燃气轮机发电机组的开发与应用——张树治
·有数据绑定、排序、查找功能的ListView(二):-.NET教程,数据库应用
·显示日期格式的几个函数-ASP教程,ASP应用
·在.NET下获取硬盘序列号的问题-.NET教程,.NET Framework
·use MSAgent in C#-.NET教程,C#语言
·利用光缆传输有线电视信号
·电信级宽带应用网站并发性承载均衡研究
最新文章
·photoshop照片处理:色彩的强化及优化_photoshop教程
·献给想我一样初基础网站优化的朋友们_seo网站优化
·百度联盟认证计划 与合作伙伴互惠共赢_网赚技巧
·如何去选择广告联盟和联盟广告_网赚技巧
·专业的网站流量快速提升的方法_站长心得
·改进网络营销的10种方法_站长心得
·教你最大限度地提高 google adsense 收入的技巧_google推广
·如何建立网站地图(site map)_站长心得
·seo偏门绝技之关键词隐身大法_seo网站优化
·265步hao123后尘被清空数据,谁来代替alexa排名?_alexa排名
相关主题
  • 用jsp实现sqlserve数据库的备份和恢复_jsp技巧
  • 用jsp实现类似资源管理器式的文件与目录的折叠(代码示例)_jsp技巧
  • 用jsp实现dos形式缓冲网页_jsp技巧
  • 用JSP实现类似资源管理器式的文件与目录的折叠-JSP教程,Jsp/Servlet
  • 用jsp实现直接下载文件而不是在浏览器中打开的功能-JSP教程,Jsp/Servlet
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭