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条回答 默认 最新

  • wanghaolovezlq 2009-09-07 16:36
    关注

    你在页面搞个全局变量计数嘛

    [color=red]var count = 0;[/color] 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"); [color=red] input.type = "file"; input.name = "file" + count ; text.type = "text"; text.name = "key" + count ; [/color] 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); }; }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题