JSPSmart实现数据上传时file和text表单同时申请的问题
发布时间:2023-05-16 13:36:46 所属栏目:教程 来源:
导读:原来这世界,就是一个圆。自始至终我都在圆圈里运动,从起点到起点,从终点到终点.在网上搜了无数的资料,为了寻找text表单和file文件一起提交的方法,累的脑袋直响.最后回到了开始的地方.找到了那条,曾经被我忽略的代码.
原来这世界,就是一个圆。自始至终我都在圆圈里运动,从起点到起点,从终点到终点.在网上搜了无数的资料,为了寻找text表单和file文件一起提交的方法,累的脑袋直响.最后回到了开始的地方.找到了那条,曾经被我忽略的代码.原来,它可以这样简单。 好吧,我直切正题,下面的例子中有从网上哪位前辈写的内容,我只是稍加改动,写本文没有商业目的,前辈原谅我没引入你的大名啊. 程序有一个提交页面,其实用html就好了,不过原代码用的是jsp我也拿来用吧. selectfile.jsp---->web.xml >servletupload.java 基本就是这么个结构 下面是代码: //selectfile.jsp <%@ page contenttype="text/html;charset=gbk" %> <html> <head> <title>file upload</title> </head> <body> <font size="5" color="#ff0000"> <b>文件上传 - 使用jspsmart upload组件</b> </font><br> <form name="selectfile" enctype="multipart/form-data" method="post" action="servletupload"> <p>文件名称: <input type="file" name="ulfile" size="20" maxlength="80"><br> </p> <p>上传路径: <input type="text" name="path" size="30" maxlength="50"><br> </p> <p>附加内容: <input type="text" name="other" size="30" maxlength="50"><br> </p> <p> <input type="submit" value="上传"> <input type="reset" value="清除"> </p> </form> </body> </html> //servletupload.java import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import com.jspsmart.upload.*; public class servletupload extends httpservlet { private servletconfig config; /** * 初始化servlet */ final public void init(servletconfig config) throws servletexception { this.config = config; } /** * 处理get请求 */ public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { printwriter out = response.getwriter(); out.println("<html>"); out.println("<body bgcolor='white'>"); out.println("<h1>jspsmartupload : servlet sample</h1>"); out.println("<hr><br>"); out.println("the method of the html form must be post."); out.println("</body>"); out.println("</html>"); } /** * 响应post请求 */ protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { printwriter out = response.getwriter(); out.println("<html>"); out.println("<body bgcolor='white'>"); out.println("<h1>jspsmartupload : servlet sample</h1>"); out.println("<hr>"); // 变量定义 int count=0; smartupload mysmartupload = new smartupload(); try { // 初始化 mysmartupload.initialize(config,request,response); // 上载 mysmartupload.upload(); com.jspsmart.upload.file f1 = mysmartupload.getfiles().getfile(0); string name = f1.getfilename(); // system.out.println (name); // 保存上载文件到指定目录 // path为form表单提交过来的 count = mysmartupload.save(mysmartupload.getrequest().getparameter("path")); //other为form表单提交过来的 string other=mysmartupload.getrequest().getparameter("other"); //这里可以对other进行处理 //request.getparameter("path");request.gerparameter("other"); // 显示处理结果 out.println(count + " file uploaded."); } catch (exception e){ out.println("unable to upload the file.<br>"); out.println("error : " + e.tostring()); } out.println("</body>"); out.println("</html>"); } } //web.xml的配置如下: <!doctype web-app public "-//sun microsystems, inc.//dtd web application 2.3//en" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <servlet> <servlet-name>upload</servlet-name> <servlet-class>servletupload</servlet-class> </servlet> <servlet-mapping> <servlet-name>upload</servlet-name> <url-pattern>servletupload</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>selectfile.jsp</welcome-file> </welcome-file-list> </web-app> 需要在web-inf/lib中引入jspsmart这个包,上网找一下就有,很多都有的下 www.jspsmart.com 这里是他的官方网站.把编译后的class文件放到web-inf/classes下就可以运行了. 这里面用到了jspsmart提供的mysmartupload.getrequest().getparameter("other"); 这个东西,由于开始的时候觉得path地址没有必要传递就早早的把这条代码删掉了,后来就想用request.getparameter("")这个得到信息,可是总是出错.在网上找了n多文章,很多人面临同样的困难.于是想用逻辑关系把这种情况避免掉.就是用单独的form上传用另一个form往数据库里录入.可是录入的时候又得不到要上传的文件名,我是想把文件名存到数据库里的.如果一定要得的话就得放到session里去,一想这样太麻烦,弄不好还容易出bug,要是把临时信息放到数据库里去,有多人一起操作的话又是个问题,其中还遇到了想往file的属性value里写信息的问题.只能读,不能写,就是这个结果.每次都是快成功的时候就卡在这样的小地方了.于是上网查找其他组件看看能不能有相应的功能.这时候使用了fileupload这个组件,网友使用的情况来看这个也要好于jspsmart可是同样没找到getparameter这样的方法. 于是继续在网上搜,结果找到自己原来用的那段代码,发现...原来mysmartupload.getrequest().getparameter就可以实现了.巨汗啊.现在改成这个样子,可以运行了.不过也许后面还要改成其他的组件,使上传的数据更稳定一些.现在就先这样了,商务逻辑已经实现了。 (编辑:聊城站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐