社区
Web 开发
帖子详情
在servlet中怎么得到
所选择的文件名
chrisl8080
2004-09-22 02:38:48
同标题
...全文
284
5
打赏
收藏
在servlet中怎么得到<input type="FILE" />所选择的文件名
同标题
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
5 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
falcon272003
2004-09-23
打赏
举报
回复
mySmartUpload.getFiles().getFile(1).getFileName());
chrisl8080
2004-09-22
打赏
举报
回复
就在SERVLET中得到选择的文件的名字
chrisl8080
2004-09-22
打赏
举报
回复
只想得到文件名用不着这么复杂吧
gjd111686
2004-09-22
打赏
举报
回复
用JSP分析multipart/form-data基于表单的文件上传
http://blog.csdn.net/gjd111686/archive/2004/08/18/78324.aspx
<%
int iTotalByte,iTotalRead,iReadByte;
iTotalByte=request.getContentLength();
iTotalRead=0;
iReadByte=0;
byte[] Buffer=new byte[iTotalByte];
if(iTotalByte>0)
{
for(;iTotalRead<iTotalByte;iTotalRead+=iReadByte)
{
try
{
iReadByte=request.getInputStream().read(Buffer,iTotalRead,iTotalByte-iTotalRead);
}
catch(Exception e)
{
e.printStackTrace();
}
}
String strContentType=request.getContentType();
//数据处理开始
String strBuffer=new String(Buffer);
%><!--<br>表单数据:<br>strBuffer<br>--><%
String strBoundary="--"+strContentType.substring(strContentType.lastIndexOf("=")+1,strContentType.length());
String strArray[]=strBuffer.split(strBoundary);
String strSubString;
int iBegin,iEnd;
iBegin=0;iEnd=0;
String strFieldName="";
String strFieldValue="";
String strFilePath="";
String strFileName="";
String strFileType="";
boolean bTrue;
bTrue=false;
int iLocation=0;
for(int iIndex=1;iIndex<strArray.length-1;iIndex++)
{
strSubString=strArray[iIndex];
iBegin=strSubString.indexOf("name=\"",0);
if(iBegin!=-1)
{
strFieldName="";strFieldValue="";
strFilePath="";strFileName="";strFileType="";
iEnd=strSubString.indexOf("\"",iBegin+6);
strFieldName=strSubString.substring(iBegin+6,iEnd);
iBegin=strSubString.indexOf("filename=\"",0); if(iBegin!=-1)
{
bTrue=true;
}
iEnd=strSubString.indexOf("\r\n\r\n",0);
if(bTrue==true)
{
//文件路径
strFilePath=strSubString.substring(iBegin+10,strSubString.indexOf("\"",iBegin+10));strFileName=strFilePath.substring(strFilePath.lastIndexOf("\\")+1);
strFileType=strSubString.substring(strSubString.indexOf("Content-Type: ")+14,strSubString.indexOf("\r\n\r\n"));
%><!--<br>文件类型:<br>strFileType<br>--><%
//文件数据
iBegin=strSubString.indexOf("\r\n\r\n",iBegin);
strFieldValue=strSubString.substring(iBegin+4);
strFieldValue=strFieldValue.substring(0,strFieldValue.lastIndexOf("\n")-1);
%><!--<br>文件路径:<br>strFilePath<br>文件名称:<br>strFileName<br>--><%
byte[] pFile=strFieldValue.getBytes();
byte[] pFileExtend=new byte[pFile.length];
iLocation=strBuffer.indexOf("filename=\"",iLocation);
for(int kIndex=iLocation;kIndex<iTotalByte-2;kIndex++)
{
if(Buffer[kIndex]==13&&Buffer[kIndex+2]==13)
{iLocation=kIndex+4;break;}
}
for(int nIndex=0;nIndex<pFile.length;nIndex++)
{
pFileExtend[nIndex]=Buffer[iLocation+nIndex];
}
/*
//保存到Local Disk;
FileOutputStream pFileOutputStream=new FileOutputStream("F:\\Site_App\\UploadFile\\"+strFileName);
pFileOutputStream.write(pFileExtend);
pFileOutputStream.close();
*/
session.putValue(strFieldName+"_FileType",strFileType);
session.putValue(strFieldName+"_FilePath",strFilePath);
session.putValue(strFieldName+"_FileName",strFileName);
session.putValue(strFieldName,pFileExtend);
}
else
{
strFieldValue=strSubString.substring(iEnd+4);
strFieldValue=strFieldValue.substring(0,strFieldValue.lastIndexOf("\n")-1);
session.putValue(strFieldName,strFieldValue);
}
bTrue=false;
}
%><!--<br>表单域名:<br>strFieldName<br>表单域值:<br>strFieldValue<br>--><%
}
//数据处理结束
}
%>
这样(String)session.getValue("表单域名")返回表单域值,而(byte[])session.getValue("File上传控件域名")返回的字节数组就可以用new ByteArrayInputStream(byte[])调用updateBinaryStream来更新到数据库了
flyxxxxx
2004-09-22
打赏
举报
回复
要用文件上传(如jspsmartupload)
文件的上传-
servlet
实现文件上传---核心API—Disk
File
ItemFactory
本文介绍Java Web
中
的文件上传实现过程,包括使用<
input
type
=
file
>标签添加上传输入项及通过Commons-
file
upload组件处理文件上传的方法。并提供了一个具体的文件上传
Servlet
示例。
JavaWeb技术实现文件上传思路
本文详细介绍了在JSP页面
中
实现文件上传的过程,包括使用<
input
type
=
file
>标签及multipart/form-data编码方式,通过
Servlet
处理上传请求,利用Disk
File
ItemFactory和
File
Upload解析上传文件,最终将文件保存至服务器并封装数据。
input
file
文件上传简单示例
本文介绍了一种使用HTML表单和JavaScript实现的图片上传方法,通过设置enc
type
属性为multipart/form-data,允许用户直接上传图片,同时携带额外的文本信息。文章详细描述了前后端的实现过程,包括如何在前端收集图片和文本信息,以及在后端如何解析这些信息并处理图片上传。
SpringMVC上传文件的三种方式
本文介绍了使用SpringMVC进行文件上传的三种方法,并对比了它们的效率。具体包括使用流上传、使用`transferTo`方法和使用SpringMVC内置方法进行文件上传的过程。
Web 开发
81,111
社区成员
341,727
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章