社区
Web 开发
帖子详情
在servlet中怎么得到
所选择的文件名
chrisl8080
2004-09-22 02:38:48
同标题
...全文
256
5
打赏
收藏
在servlet中怎么得到<INPUT TYPE="FILE">所选择的文件名
同标题
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用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)
input
file
图片上传即时预览
在JSP
中
,我们可以使用`<form>`元素配合`<
input
type
="
file
">`提交文件,然后通过
Servlet
或Controller接收并保存图片。 ```jsp <form action="upload
Servlet
" method="post" enc
type
="multipart/form-data"> ...
struts2
中
文
文件名
文件下载
<
input
type
="
file
" name="
file
"/> <
input
type
="submit" value="下载"/> </form> ``` 4. **服务器环境配置**: 确保服务器(如Tomcat)的字符编码设置支持UTF-8,避免在接收请求时出现乱码。 通过以上步骤,...
servlet
实现文件上传下载
在JSP页面
中
,我们创建了一个HTML表单,用户可以
选择
多个文件进行上传(通过`<
input
type
="
file
" name="
file
1">`等元素)。此外,还有一个链接用于文件下载,通过`<a>`标签指向一个
Servlet
,并将
文件名
作为参数传递...
最简单的多文件上传 jsp
首先,我们需要在HTML表单
中
添加多个`<
input
type
="
file
">`元素,让用户可以
选择
多个文件进行上传: ```html <form action="upload
Servlet
" method="post" enc
type
="multipart/form-data"> <
input
type
="
file
" ...
commons-
file
upload-1.2.jar和commons-io-1.3.2.jar
第1个上传组件commons-
file
upload =============commons-
file
upload ============... <tr><td align="center"><
input
name="upload"
type
="submit" value="开始上传"/></td></tr> </table> </form> </body> </html>
Web 开发
81,122
社区成员
341,743
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章