glys2050 2009-09-07 15:55
浏览 231
已采纳

批量上传处理问题

我现在用JSP做一个在线考试系统,批量上传一些语音文件,每个语音文件都对应一个文本框来输入对应考题的答案,以下是我写的上传的表单页面,已可以实现文件域和文本框的对应增删,但是不知道该如何写上传的处理程序,主要是文件个数的获取和对应文本框的值获取的问题,想求一个相关的实例。先谢过了哦!



语音文件: 答 案:
function AddMore(){ var more = document.getElementById("file"); var br = document.createElement("br"); var input = document.createElement("input"); var text = document.createElement("input"); var button = document.createElement("input"); input.type = "file"; input.name = "file"; text.type = "text"; text.name = "key"; button.type = "button"; button.value = "删除"; more.appendChild(br); more.appendChild(input); more.appendChild(text); more.appendChild(button); button.onclick = function(){ more.removeChild(br); more.removeChild(input); more.removeChild(text); more.removeChild(button); }; }



刚刚从网上又当了个批量上传的程序,自己改了一下,但是总是出现NullPointerException的异常,贴出来麻烦大家帮忙改改啊。本人比较穷,就这么点分,希望大家不要嫌弃啊...
<%@page contentType="text/html"%>
<%@page pageEncoding="GB2312"%>
<%@page import="java.util.*" %>
<%@page import="java.io.*" %>

<%

request.setCharacterEncoding("GBK");
response.setContentType("text/html;charset=GB2312");
char[] hchl={13,10};
String boundary=request.getContentType().substring(30);
String field_boundary="--"+boundary+new String(hchl);
String last_boundary="--"+boundary+"--"+new String(hchl);
String _msg="";
ServletInputStream getdata=request.getInputStream();
ByteArrayOutputStream temp=new ByteArrayOutputStream();
byte[] data_line=new byte[8192];
int line_byte_count=0;
boolean found_boundary=false;
String [] txtKey = request.getParameterValues("key");
int i = 0;
while((line_byte_count=getdata.readLine(data_line,0,data_line.length))!=-1){
if(!found_boundary){
line_byte_count=getdata.readLine(data_line,0,data_line.length);
}
String temp_str=new String(data_line,0,line_byte_count);
if(temp_str.indexOf("filename")!=-1){
if(temp_str.substring(temp_str.indexOf("filename=")+9,temp_str.lastIndexOf("\"")+1).length()>2){
String file_name=temp_str.substring(temp_str.lastIndexOf("\")+1,temp_str.lastIndexOf("\""));
line_byte_count=getdata.readLine(data_line,0,data_line.length);
line_byte_count=getdata.readLine(data_line,0,data_line.length);
String path = pageContext.getServletContext().getRealPath("\VedioTest\vedios\");
FileOutputStream myfile=new FileOutputStream(path+"/"+file_name,false); //文件存放目录
//String savePath = "\vedios\"+file_name;
connDbBean.executeUpdate("insert into vedio(vedio,key) values('"+file_name+"','"+txtKey[i]+"')");
i++;
boolean test=true;
while(test)
{
line_byte_count=getdata.readLine(data_line,0,data_line.length);
if(line_byte_count==-1){
test=false;
break;
}
if(temp.size()==0){
temp.write(data_line,0,line_byte_count);
}
else{
if(new String(data_line,0,line_byte_count).equals(field_boundary) || new String(data_line,0,line_byte_count).equals(last_boundary)){
myfile.write(temp.toByteArray(),0,temp.toByteArray().length-2);
temp.reset();
myfile.close();
//out.println(file_name+"上传成功了
");
_msg=_msg+file_name+"上传成功
";
test=false;
found_boundary=true;
}
else{
temp.writeTo(myfile);
temp.reset();
temp.write(data_line,0,line_byte_count);
}
}
}
}
else{
String field_name=temp_str.substring(temp_str.indexOf("name")+6,temp_str.lastIndexOf(";")-1);
line_byte_count=getdata.readLine(data_line,0,data_line.length);
line_byte_count=getdata.readLine(data_line,0,data_line.length);
line_byte_count=getdata.readLine(data_line,0,data_line.length);
line_byte_count=getdata.readLine(data_line,0,data_line.length);
found_boundary=true;
// out.println(field_name+"没有选择上传文件!
");
// _msg=_msg+field_name+"没有选择上传文件
";
}
}
else{
String field_name=temp_str.substring(temp_str.indexOf("name")+6,temp_str.lastIndexOf("\""));
line_byte_count=getdata.readLine(data_line,0,data_line.length);
temp.reset();
boolean test=true;
while(test) {
line_byte_count=getdata.readLine(data_line,0,data_line.length);
if(line_byte_count==-1){
test=false;
break;
}
if(new String(data_line,0,line_byte_count).equals(field_boundary) || new String(data_line,0,line_byte_count).equals(last_boundary)){
test=false;
found_boundary=true;
if(temp.size()>2){
// out.println(field_name+":"+new String(temp.toByteArray())+"
");
_msg=_msg+field_name+":"+new String(temp.toByteArray())+"
";
}
else{
// out.println(field_name+"没有内容!
");
_msg=_msg+field_name+"没有内容!
";
}
temp.reset();
}
else{
temp.write(data_line,0,line_byte_count);
}
}
}

}
getdata.close();
out.println("window.parent.Finish(&#39;&quot;+_msg+&quot;&#39;);");
%>
[b]问题补充:[/b]
但是我的文件域和文本框都是动态生成的,我无法确定个数啊,那样该怎么依次取名呢?
[b]问题补充:[/b]
但是我的文本域和文本框都是动态生成的,不能确定个数,那样该如何依次取名呢?

  • 写回答

2条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 苹果手机删除了qq软件,qq文件怎么恢复
      • ¥20 用mips 人力编程排序问题(能在mars按要求运行)
      • ¥15 Verilog小型神经网络(单层反馈神经网络)
      • ¥15 设计一个数字钟,实现时分秒的显示
      • ¥15 Clion使用头文件链接两个cpp
      • ¥15 51单片机无法触发外部中断问题
      • ¥15 基于半监督,无监督的调制识别
      • ¥15 cst求 阵列 rcs 时候 频域 时域求解器 应该怎样设置 才让两者 计算结果 相似 请附图
      • ¥15 关于#51单片机#的问题:大家帮我看一下为什么我的计时器数字一直不稳定
      • ¥15 Qt 在应用程序里总是有[PREVIEW] recv window msg, name:(Qt5QWindowIcon) hwd:(287097a)等信息不断输出,怎么解决