社区
Web 开发
帖子详情
在servlet中怎么得到
所选择的文件名
chrisl8080
2004-09-22 02:38:48
同标题
...全文
255
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实现
input
标签
file
类型的上传图片即时预览功能
struts2
中
文
文件名
文件下载
解决
中
文
文件名
问题,使用解决方法,
文件名
编码:ISO8859-1
servlet
实现文件上传下载
用最简洁的代码 实现 java 文件上传 下载 ,适合于新手学习。
最简单的多文件上传 jsp
修改
file
.jsp
中
的上传目录位置就可以了,最简单也不要组件
commons-
file
upload-1.2.jar和commons-io-1.3.2.jar
第1个上传组件commons-
file
upload =============commons-
file
upload ================ common-
file
upload组件是apache的一个开源项目之一,可以从http://jakarta.apache.org/commons/
file
upload/下载。该组件简单易用,可实现一次上传一个或多个文件,并可限制文件大小。 -下载后解压zip包,将commons-
file
upload-1.1.1.jar,和commons-io-1.2.jar(这里我们用的是更新的版本,但是用法是一样的)复制到tomcat的webapps\你的webapp\WEB-INF\lib\下,如果目录不存在请自建目录。 新建一个
servlet
:
File
Upload.java用于文件上传: package com.drp.util.
servlet
; import java.io.IOException; import java.io.PrintWriter; import javax.
servlet
.
Servlet
Exception; import javax.
servlet
.http.Http
Servlet
; import javax.
servlet
.http.Http
Servlet
Request; import javax.
servlet
.http.Http
Servlet
Response; import org.apache.commons.
file
upload.*; import java.util.*; import java.util.regex.*; import java.io.*; import org.apache.commons.
file
upload.
servlet
.*; import org.apache.commons.
file
upload.disk.Disk
File
ItemFactory; public class
File
Upload extends Http
Servlet
{ private String uploadPath = ""; // 用于存放上传文件的目录 private
File
tempPath = new
File
("D:\\Tomcat 5.5\\webapps\\drp1.2\\tempimages\\"); // 用于存放临时文件的目录 public void doPost(Http
Servlet
Request req, Http
Servlet
Response res) throws
Servlet
Exception, IOException { res.setContent
Type
("text/html; charset=GB18030"); PrintWriter out = res.getWriter(); System.out.println(req.getContentLength()); System.out.println(req.getContent
Type
()); Disk
File
ItemFactory factory = new Disk
File
ItemFactory(); // maximum size that will be stored in memory //允许设置内存
中
存储数据的门限,单位:字节 factory.setSizeThreshold(4096); // the location for saving data that is larger than getSizeThreshold() //如果文件大小大于SizeThreshold,则保存到临时目录 factory.setRepository(new
File
("D:\\Tomcat 5.5\\webapps\\drp1.2\\tempimages"));
Servlet
File
Upload upload = new
Servlet
File
Upload(factory); // maximum size before a
File
UploadException will be thrown //最大上传文件,单位:字节 upload.setSizeMax(1000000); try { List
file
Items = upload.parseRequest(req); // assume we know there are two
file
s. The first
file
is a small //
Web 开发
81,122
社区成员
341,744
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章