在上传图片时如何获得上传图片的文件名,并写入数据库
upload.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN ">
<html>
<head>
<title> 文件上传 </title>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
</head>
<body>
<p> </p>
<p align= "center "> 上传文件选择 </p>
<FORM METHOD= "POST " ACTION= "do_upload.jsp "
ENCTYPE= "multipart/form-data ">
<input type= "hidden " name= "TEST " value= "good ">
<table width= "75% " border= "1 " align= "center ">
<tr>
<td> <div align= "center "> 请选择上传的图片:
<input type= "FILE " name= "FILE1 " size= "30 ">
</div> </td>
</tr>
<tr>
<td> <div align= "center ">
<input type= "submit " name= "Submit " value= "上传它! ">
</div> </td>
</tr>
</table>
</FORM>
</body>
</html>
do_upload.jsp
<%@ page contentType= "text/html;charset=gb2312 "%>
<%@ page import= "java.sql.* "%>
<%@ page import= "com.jspsmart.upload.* " %>
<% //Integer ii=(Integer)session.getAttribute( "filename ");
// int filename=ii.intValue();
// int filename1=filename+1;
// session.setAttribute( "filename ",new Integer(filename1));
// System.out.println((((Integer)session.getAttribute( "filename ")).intValue()));
String myFileName = " ";
//实例化上载bean
com.jspsmart.upload.SmartUpload mySmartUpload=new com.jspsmart.upload.SmartUpload();
//初始化
mySmartUpload.initialize(pageContext);
//设置上载的最大值
mySmartUpload.setMaxFileSize(500*1024*1024);
//上载文件
mySmartUpload.upload();
//循环取得所有上载的文件
for (int i=0;i <mySmartUpload.getFiles().getCount();i++){
//取得上载的文件
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing())
{
//取得上载的文件的文件名
myFileName=myFile.getFileName();
//取得不带后缀的文件名
String suffix=myFileName.substring(0,myFileName.lastIndexOf( '. '));
//取得后缀名
String ext= mySmartUpload.getFiles().getFile(0).getFileExt();
//取得文件的大小
int fileSize=myFile.getSize();
//保存路径
String aa=getServletContext().getRealPath( "/ ")+ "qy\\upload\\ ";
String trace=aa+myFileName;
//System.out.println(trace);
//取得别的参数
String explain=(String)mySmartUpload.getRequest().getParameter( "text ");
String send=(String)mySmartUpload.getRequest().getParameter( "send ");
//将文件保存在服务器端
myFile.saveAs(trace,mySmartUpload.SAVE_PHYSICAL);
//下面的是将上载的文件保存到数据库中
//将文件读到流中
out.println(( "上载成功!!! ").toString());
}
else
{ out.println(( "上载失败!!! ").toString()); }
}//与前面的if对应
String type=(String)session.getAttribute( "type ");
//System.out.println(type);
%>
<form name= "form1 " method= "post " action= "add_xuanzeti.jsp ">
<input type= "hidden " name= "type " value= " <%=type%> ">
<input type= "hidden " name= "name " value= " <%=myFileName%> ">
<input type= "submit " name= "Submit " value= "关闭 " onClick= "window.close() ">
</form>
我能把图片上传到指定文件夹中,问题是我如何在上传的同时获得上传的图片名,并写入数据库呢?我想在前台显示我上传的图片!请各位请各位兄弟姐妹,同志朋友帮我解决这个问题!我会以各位的热情真诚为各位朋友解决问题的!谢谢!
[解决办法]
//取得上载的文件的文件名
myFileName=myFile.getFileName();
这不是你要的文件名吗??