社区
Web 开发
帖子详情
请问,JSP如何得到
,输入的文件名称文本字符串呢?
yyhyan
2004-09-22 10:43:06
request.getParameter("FILE1")
不行啊
...全文
1454
8
打赏
收藏
请问,JSP如何得到<INPUT TYPE="FILE" NAME="FILE1" SIZE="50">,输入的文件名称文本字符串呢?
request.getParameter("FILE1") 不行啊
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
8 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
jackcf
2004-09-23
打赏
举报
回复
不要乱说,你们有没有试过,是真的得不到吗,我为什么可以得到呀,呵呵
还说的很好道理一样的,什么二进制呀?!
shaopin
2004-09-23
打赏
举报
回复
如果用JspSmartUpload组件,参照
http://www.ccw.com.cn/htm/app/aprog/01_5_30_2.asp
可以取出文件名
shaopin
2004-09-23
打赏
举报
回复
直接用request.getParameter是取不到的,别不信
方法有2:
1、直接用url方式传递文件名
2、参照 gjd111686(数字金刚)的方法可以取得文件名
gjd111686
2004-09-23
打赏
举报
回复
用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来更新到数据库了
xunyiren
2004-09-23
打赏
举报
回复
//通过隐藏域实现
<script language="javascript">
function check(obj)
{
if (obj.upfile.value == "")
{
alert("请选择系统文件!");
return false;
}
document.form1.submit();
}
</script>
<form name="form1" action="upload.jsp" onsubmit="return check(this)">
<!--这里让用户只能选择文件,不能输入\粘贴-->
<input type="file" name="upfile" onKeyPress="return false;" onPaste="return false;" onpropertychange="this.form.filename.value=this.value;alert(this.form.filename.value);">
<input type="hidden" name="filename" value="">
<input type="submit" name="sub" value="确定">
在upload.jsp页面只需:
String filedir=request.getParameter("filename");即可获得客户选择的文件名称
kxx129
2004-09-23
打赏
举报
回复
第一个页面:
<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<BODY>
<P>选择要上传的文件:<BR>
<FORM action="accept.jsp" method="post" ENCTYPE="multipart/form-data">
<INPUT type=FILE name="boy" size="38">
<BR>
<INPUT type="submit" name ="g" value="提交">
</BODY>
</HTML>
第二个页面:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<BODY>
<%try{ //用客户的session的id建立一个临时文件:
String tempFileName=(String)session.getId();
//建立临时文件f1:
File f1=new File("G:/JSP/adou/img/member/",tempFileName);
FileOutputStream o=new FileOutputStream(f1);
//将客户上传的全部信息存入f1:
InputStream in=request.getInputStream();
byte b[]=new byte[10000];
int n;
while( (n=in.read(b))!=-1)
{o.write(b,0,n);
}
o.close();in.close();
//读取临时文件f1,从中获取上传文件的名字,和上传的文件的内容:
RandomAccessFile random=new RandomAccessFile(f1,"r");
//读出f1的第2行,析取出上传文件的名字:
int second=1;
String secondLine=null;
while(second<=2)
{secondLine=random.readLine();
second++;
}
//获取第2行中目录符号'\'最后出现的位置
int position=secondLine.lastIndexOf('\\');
//客户上传的文件的名字是:
String fileName=secondLine.substring(position+1,secondLine.length()-1);
random.seek(0); //再定位到文件f1的开头。
//获取第4行回车符号的位置:
long forthEndPosition=0;
int forth=1;
while((n=random.readByte())!=-1&&(forth<=4))
{ if(n=='\n')
{ forthEndPosition=random.getFilePointer();
forth++;
}
}
//根据客户上传文件的名字,将该文件存入磁盘:
File f2=new File("G:/JSP/adou/img/member/",fileName);
session.setAttribute("Name",fileName);//供showImage.jsp页面使用。
RandomAccessFile random2=new RandomAccessFile(f2,"rw");
//确定出文件f1中包含客户上传的文件的内容的最后位置,即倒数第6行。
random.seek(random.length());
long endPosition=random.getFilePointer();
long mark=endPosition;
int j=1;
while((mark>=0)&&(j<=6))
{ mark--;
random.seek(mark);
n=random.readByte();
if(n=='\n')
{ endPosition=random.getFilePointer();
j++;
}
}
//将random流指向文件f1的第4行结束的位置:
random.seek(forthEndPosition);
long startPoint=random.getFilePointer();
//从f1读出客户上传的文件存入f2(读取从第4行结束位置和倒数第6行之间的内容)。
while(startPoint<endPosition-1)
{ n=random.readByte();
random2.write(n);
startPoint=random.getFilePointer();
}
random2.close();random.close();
f1.delete(); //删除临时文件
}
catch(IOException ee){}
out.print("文件已上传");
%>
</BODY>
</HTML>
guojiafuzhuxi
2004-09-23
打赏
举报
回复
没去想,UP
whirlsun
2004-09-23
打赏
举报
回复
当然,不行了,那是二进制喽,你那样是不能读出来的
解析 Firebase App Check 所包含的谷歌内部核心组件
【源码预览】:https://renmaiwang.cn/s/j2uo7 Firebase App Check 这一工具所对应的谷歌内部核心组成部分。
chromedriver-mac-x64-142.0.7423.0(Canary).zip
chromedriver-mac-x64-142.0.7423.0(Canary).zip
EXCEl模板:小组工作梳理表.xlsx
EXCEl模板:小组工作梳理表.xlsx
Android Studio Narwhal 2025.1.3(android-studio-2025.1.3.7-mac.zip.002)
Android Studio Narwhal 2025.1.3(android-studio-2025.1.3.7-mac.dmg)适用于macOS Intel系统,文件使用360压缩软件分割成两个压缩包,必须一起下载使用: part1: https://download.csdn.net/download/weixin_43800734/91972217 part2: https://download.csdn.net/download/weixin_43800734/91972215
介绍 Rust 编程语言下的极简主义机器学习框架
【源码预览】:https://renmaiwang.cn/s/xj56i (最新版、最全版本)介绍 Rust 编程语言下的极简主义机器学习框架
Web 开发
81,117
社区成员
341,740
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章