社区
Web 开发
帖子详情
在servlet中怎么得到
所选择的文件名
chrisl8080
2004-09-22 02:38:48
同标题
...全文
241
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)
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 //
<
input
type
='
file
'>
选择
文件后自动上传文件
想要一个
选择
了文件就自动上传的效果,但之前的 都是写在form表单
中
来提交的。借助jquery的ajax
file
upload.js,可以这样子弄: 正面: function upload
File
(obj,
type
) { $.ajax
File
Upload({ url : "http://localhost:8081/ws2/
servlet
/
file
Upload", secur
struts2文件下载
type="stream">
application/octet-stream 比如这里就是 get
Input
Stream() 来获取下载文件的内容,意味着你的 Action 要有这个方法 -->
input
Stream
JSP上传文件代码
一、上传表单
<%@ page content
Type
="text/html;charset=gb2312" %>
上传文件
上传文件
文件的上传和下载
一、如何实现文件上传 实现 Web开发
中
的文件上传功能,通常需完成两步操作:一是在Web页面
中
添加上传输入项;二是在
Servlet
中
读取上传文件的数据,并保存到本地硬盘
中
。 由于大多数文件的上传都是通过表单的形式提交给服务器的,因此,要想在程序
中
实现文件上传的功能,首先要创建一个用于提交上传文件的表单页面。在页面
中
,需要使用<
input
type
="
file
">标签在Web页面
中
添加文件上传输入项。 <
input
type
="
file
">标签的使...
Web 开发
81,090
社区成员
341,731
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章