<%@page contentType="text/html"%>
<%@page pageEncoding="GB2312"%>
<%@page import="java.util.*" %>
<%@page import="java.io.*" %>
<%
//jsp文件上传源码,单一或多文件也可与其他标单类型混杂使用,可不限制上传大小,速度一流
//作者:sgzyl 2007.10.26
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);
ServletInputStream getdata=request.getInputStream();
ByteArrayOutputStream temp=new ByteArrayOutputStream();
byte[] data_line=new byte[8192];
int line_byte_count=0;
boolean found_boundary=false;
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);
FileOutputStream myfile=new FileOutputStream("d:\\"+file_name,false);
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+"上传成功了<br>");
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+"没有选择上传文件!<br>");
}
} 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())+"<br>");
}else{
out.println(field_name+"没有内容!<br>");
}
temp.reset();
}else{
temp.write(data_line,0,line_byte_count);
}
}
}
}
getdata.close();
%>
<%@page pageEncoding="GB2312"%>
<%@page import="java.util.*" %>
<%@page import="java.io.*" %>
<%
//jsp文件上传源码,单一或多文件也可与其他标单类型混杂使用,可不限制上传大小,速度一流
//作者:sgzyl 2007.10.26
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);
ServletInputStream getdata=request.getInputStream();
ByteArrayOutputStream temp=new ByteArrayOutputStream();
byte[] data_line=new byte[8192];
int line_byte_count=0;
boolean found_boundary=false;
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);
FileOutputStream myfile=new FileOutputStream("d:\\"+file_name,false);
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+"上传成功了<br>");
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+"没有选择上传文件!<br>");
}
} 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())+"<br>");
}else{
out.println(field_name+"没有内容!<br>");
}
temp.reset();
}else{
temp.write(data_line,0,line_byte_count);
}
}
}
}
getdata.close();
%>
解决方案 »
- 大家开发ssh,用的啥数据列表组件?
- 关于java web的学习请教
- servlet中能否检测到客户端的连接速度
- Response.AddHeader("Content-Disposition", "attachment;filename="+ fileName)
- 请问jsp程序员的工资是多少呀???最好是把地址写下,谢谢
- ###要在JSP中只显示数据库中的最后一条纪录.应该如何解决?多谢!!###
- IBM30G硬盘+30G经典电影 求 页面抽取问题!
- 为什么会这样呢?急!高分求救……
- 在MVC架构的系统里,是不是不应该出现www.x.com/x/x.jsp这样形式的页面?
- 求助 freemarker在springmvc下可以直接访问后台静态方法吗?
- 请问下我的jsp页面里的js验证为什么没有作用呢?是错在哪里了呢?
- jsp转servelt,servlet转到jsp后如何获得参数?
很好用
借来用了
嘿嘿