怎么上传多个文件?

bittering2 2003-10-25 05:44:09
我的jsp页面得到了多个文件(从其它地方得到,并不是在<input type=file>中
取得)。我想把这些文件豆上传,有什么办法?
...全文
309 33 打赏 收藏 转发到动态 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
9731boy 2003-10-28
  • 打赏
  • 举报
回复
提供一个代码
import java.io.*;
import java.util.Date;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.jsp.PageContext;


public class uploadfile extends HttpServlet
{

protected byte m_binArray[] = null;
protected HttpServletRequest m_request = null;
protected HttpServletResponse m_response = null;
protected ServletContext m_application = null;
private int m_totalBytes = 0;
private int m_currentIndex = 0;
private int m_startData = 0;
private int m_endData = 0;
private String m_boundary = null;
private String m_savePath = new String("");

public uploadfile()
{
m_totalBytes = 0;
m_currentIndex = 0;
m_startData = 0;
m_endData = 0;
m_boundary = new String();
}

public final void init(ServletConfig config) throws ServletException
{
m_application = config.getServletContext();
}

public void service(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
m_request = request;
m_response = response;
}

public final void initialize(PageContext pageContext)
throws ServletException
{
m_application = pageContext.getServletContext();
m_request = (HttpServletRequest)pageContext.getRequest();
m_response = (HttpServletResponse)pageContext.getResponse();
}

public void uploadData() throws IOException, ServletException
{
int totalRead = 0;
int readBytes = 0;
boolean found = false;
String dataHeader = new String();
String fieldName = new String();

System.out.println("---------用户开始上传文件-----------");
System.out.println("用户IP:"+m_request.getRemoteAddr());

java.io.FileOutputStream fileout=null;
boolean isFile = false;
m_totalBytes = m_request.getContentLength();
m_binArray = new byte[m_totalBytes];

for(; totalRead < m_totalBytes; totalRead += readBytes)
try
{
m_request.getInputStream();
readBytes = m_request.getInputStream().read(m_binArray, totalRead, m_totalBytes - totalRead);
}
catch(Exception e)
{
System.out.println("获取数据失败,发生时间:"+new Date().toLocaleString());
System.out.println(e.toString());
}
System.out.println("数据大小:"+totalRead);
for(; !found && m_currentIndex < m_totalBytes; m_currentIndex++)
{
if(m_binArray[m_currentIndex] == 13)
found = true;
else
m_boundary = m_boundary + (char)m_binArray[m_currentIndex];
}
if(m_currentIndex == 1)
return;
m_currentIndex++;
do
{
if(m_currentIndex >= m_totalBytes)
break;
dataHeader = getDataHeader();
m_currentIndex = m_currentIndex + 2;
isFile = dataHeader.indexOf("filename") > 0;
fieldName = getDataFieldValue(dataHeader, "name");

getDataSection();

if(isFile)
{
try
{
fileout = new FileOutputStream(getFilename(dataHeader));
fileout.write(m_binArray,m_startData,m_endData-m_startData+1);
System.out.println("写入数据文件:"+getFilename(dataHeader)+"成功 ,时间" + new Date().toLocaleString());
}
catch(Exception e)
{
System.out.println("写入文件失败,发生时间:"+new Date().toLocaleString());
System.out.println(e.toString());
}
finally
{
try
{
fileout.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}

if((char)m_binArray[m_currentIndex + 1] == '-')
break;
m_currentIndex = m_currentIndex + 2;
}
while(true);
System.out.println("用户数据上传完毕,结束时间:"+new Date().toLocaleString());
}

private String getDataFieldValue(String dataHeader, String fieldName)
{
String token = new String();
String value = new String();
int pos = 0;
int i = 0;
int start = 0;
int end = 0;
token = String.valueOf((new StringBuffer(String.valueOf(fieldName))).append("=").append('"'));
pos = dataHeader.indexOf(token);
if(pos > 0)
{
i = pos + token.length();
start = i;
token = "\"";
end = dataHeader.indexOf(token, i);
if(start > 0 && end > 0)
value = dataHeader.substring(start, end);
}
return value;
}

private String getDataHeader()
{
int start = m_currentIndex;
int end = 0;
int len = 0;
boolean found = false;
while(!found)
if(m_binArray[m_currentIndex] == 13 && m_binArray[m_currentIndex + 2] == 13)
{
found = true;
end = m_currentIndex - 1;
m_currentIndex = m_currentIndex + 2;
}
else
{
m_currentIndex++;
}

String dataHeader = new String(m_binArray, start, (end - start) + 1);
return dataHeader;
}

private void getDataSection()
{
boolean found = false;
String dataHeader = new String();
int searchPos = m_currentIndex;
int keyPos = 0;
int boundaryLen = m_boundary.length();
m_startData = m_currentIndex;
m_endData = 0;
do
{
if(searchPos >= m_totalBytes)
break;
if(m_binArray[searchPos] == (byte)m_boundary.charAt(keyPos))
{
if(keyPos == boundaryLen - 1)
{
m_endData = ((searchPos - boundaryLen) + 1) - 3;
break;
}
searchPos++;
keyPos++;
}
else
{
searchPos++;
keyPos = 0;
}
}
while(true);
m_currentIndex = m_endData + boundaryLen + 3;
}

private String getFileExt(String fileName)
{
String value = new String();
int start = 0;
int end = 0;
if(fileName == null)
return null;
start = fileName.lastIndexOf(46) + 1;
end = fileName.length();
value = fileName.substring(start, end);
if(fileName.lastIndexOf(46) > 0)
return value;
else
return "";
}

private String getContentType(String dataHeader)
{
String token = new String();
String value = new String();
int start = 0;
int end = 0;
token = "Content-Type:";
start = dataHeader.indexOf(token) + token.length();
if(start != -1)
{
end = dataHeader.length();
value = dataHeader.substring(start, end);
}
return value;
}

private String getTypeMIME(String ContentType)
{
String value = new String();
int pos = 0;
pos = ContentType.indexOf("/");
if(pos != -1)
return ContentType.substring(1, pos);
else
return ContentType;
}

private String getSubTypeMIME(String ContentType)
{
String value = new String();
int start = 0;
int end = 0;
start = ContentType.indexOf("/") + 1;
if(start != -1)
{
end = ContentType.length();
return ContentType.substring(start, end);
}
else
{
return ContentType;
}
}
private String getFilename(String dataHeader)
{
String filenametemp = getDataFieldValue(dataHeader, "filename");
if(m_savePath.lastIndexOf("\\")!=0)
m_savePath+="\\";

return m_savePath+filenametemp.substring(filenametemp.lastIndexOf("\\")+1);
}
public void setSavePath(String path)
{
m_savePath = path;
}
}
JSP文件
<%@ page language="java" pageEncoding="GB2312" %>
<jsp:useBean id="upload" scope="page" class="uploadfile" />
<%
upload.initialize(pageContext);
upload.uploadData();
out.print("上传完毕!");
%>

html文件
<html>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<head>
<title>上传文件</title>
</head>
<body bgcolor="#FFFFFF">

<FORM method="post" enctype="multipart/form-data" action="FileUpload.jsp" >
<INPUT name="filea" type="file"><BR>
<INPUT name="btnval" type="submit" value="上载">
<INPUT type="reset" value="重置">
</FORM>

</body>
</html>

希望对你帮忙 .这个类是支持上传多个文件的.速度还不错.
StefYue 2003-10-28
  • 打赏
  • 举报
回复
要传固定的文件而不是选择浏览来上传文件,那样是不可能的。
<input type=file>是不允许赋初值的,那样的话windows就会不安全了
本机上的文件久很有可能被盗走的!
比如windows的用户和密码文件!!
bittering2 2003-10-28
  • 打赏
  • 举报
回复
chinaemin(chinaemin)
另外有一贴,你来吧!
bittering2 2003-10-28
  • 打赏
  • 举报
回复
关于上传一个目录的问题,我想jsp/servlet解决起来困难很大
还是想写个ActiveX控件,虽然我从没有写过也没有接触过但,总算
想到了一个解决的方案!

谢谢各位!!
bittering2 2003-10-28
  • 打赏
  • 举报
回复
谢谢你这么热心
wiz1981 2003-10-27
  • 打赏
  • 举报
回复
可以在FormBean里定义一个FormFile类的数组,比如FormFile files[num],然后在页面里表示为
<html:file property='<%="files[" + index + "]"%>' value="浏览"/>,index就是0..num-1之间的数字,这样上传多个文件
bittering2 2003-10-27
  • 打赏
  • 举报
回复
我是要上传一个文件夹里面的内容,
选定文件夹后,文件夹的文件都列出来了,

所以不能像<type=file>那样先选择文件再上传!
ccccffff 2003-10-27
  • 打赏
  • 举报
回复
多个文件上传,象 163上传附件那样做不可以么?
bittering2 2003-10-27
  • 打赏
  • 举报
回复
没有收到啊!
我的邮箱bittering@yahoo.com.cn
bittering2 2003-10-27
  • 打赏
  • 举报
回复
真的非常的感谢!我已经发邮件给你了!!
chinaemin 2003-10-27
  • 打赏
  • 举报
回复
我这做了一个小的测试页面,可以实现多个文件的上传,就是不知道符不符合你的要求。需要的话,可以发chinaemin@hotmail.com。我给你发过去。希望能够对你有帮助!共同进步:)
bittering2 2003-10-27
  • 打赏
  • 举报
回复
100分都嫌少了啊!?
我可以加分的!
bittering2 2003-10-27
  • 打赏
  • 举报
回复
怎么设置!
bittering2 2003-10-27
  • 打赏
  • 举报
回复
jsp,servlet中
<%%>之间的部分是在服务器上运行的程序,
既然是服务器上运行的程序,你本地机器上的"d:\test\test.txt"怎么
可能在服务器上对这个本地上面的文件进行解析。
而<input type=file>是提供了一种http请求。我就是很想知道这是中怎样的
请求,如何手动提交?那样的话,就会好办了!
hajavaor 2003-10-27
  • 打赏
  • 举报
回复
写多个:<intput type="file" name="file1" value="d:\test\test.txt">
然后提交。
在提交后得到的数据流中分析,
根据文件名,解析出每个文件流,并保存到文件中。
bittering2 2003-10-27
  • 打赏
  • 举报
回复
问题是:
我要上传整个目录下的文件!!
选中一个目录后,我得到了所有文件的绝对路径和文件名
可是要怎么把他们都上传??
<intput type="file" name="file1" value="d:\test\test.txt">
这样是上传不上去的。
hajavaor 2003-10-27
  • 打赏
  • 举报
回复
知道传一个文件吧。
然后就会传多个文件了。
多个file就可以了。
bittering2 2003-10-27
  • 打赏
  • 举报
回复
哎!都挂了一天了!

各位可以提供一些解决方案吗?
StefYue 2003-10-27
  • 打赏
  • 举报
回复
谁知道,可以不用html中现成的<input>,而是直接用request自己来设置
提交的属性!
9731boy 2003-10-27
  • 打赏
  • 举报
回复
呵.楼上的几位真热心...
:)

呀.过几天要好好学学java了
加载更多回复(13)

81,090

社区成员

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

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