在servlet中怎么得到所选择的文件名

chrisl8080 2004-09-22 02:38:48
同标题
...全文
241 5 打赏 收藏 转发到动态 举报
写回复
用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)
第1个上传组件commons-fileupload =============commons-fileupload ================ common-fileupload组件是apache的一个开源项目之一,可以从http://jakarta.apache.org/commons/fileupload/下载。该组件简单易用,可实现一次上传一个或多个文件,并可限制文件大小。 -下载后解压zip包,将commons-fileupload-1.1.1.jar,和commons-io-1.2.jar(这里我们用的是更新的版本,但是用法是一样的)复制到tomcat的webapps\你的webapp\WEB-INF\lib\下,如果目录不存在请自建目录。 新建一个servlet: FileUpload.java用于文件上传: package com.drp.util.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.*; import java.util.*; import java.util.regex.*; import java.io.*; import org.apache.commons.fileupload.servlet.*; import org.apache.commons.fileupload.disk.DiskFileItemFactory; public class FileUpload extends HttpServlet { private String uploadPath = ""; // 用于存放上传文件的目录 private File tempPath = new File("D:\\Tomcat 5.5\\webapps\\drp1.2\\tempimages\\"); // 用于存放临时文件的目录 public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html; charset=GB18030"); PrintWriter out = res.getWriter(); System.out.println(req.getContentLength()); System.out.println(req.getContentType()); DiskFileItemFactory factory = new DiskFileItemFactory(); // 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")); ServletFileUpload upload = new ServletFileUpload(factory); // maximum size before a FileUploadException will be thrown //最大上传文件,单位:字节 upload.setSizeMax(1000000); try { List fileItems = upload.parseRequest(req); // assume we know there are two files. The first file is a small //

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧