帮我把这个程序填完整!在最后面,是一本书上的例子!那书上没写完,谢谢!package upload;
import java.io.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletException;
public class upload{
private static String newline="\n";
private String uploadDirectory=".";
private String ContentType="";
private String CharacterEncoding="";private String getFileName(String s){
int i=s.lastIndexOf("\\");
if(i<0||i>=s.length()-1){
i=s.lastIndexOf("/");
if(i<0||i>s.length()-1)
return s;
}
return s.substring(i+1);
}public void setUploadDirectory(String s){
uploadDirectory=s;
}public void setContentType(String s){
ContentType=s;
int j;
if((j=ContentType.indexOf("boundary="))!=-1){
ContentType=ContentType.substring(j+9);
ContentType="--"+ContentType;
}
}public void setCharacterEncoding(String s){
CharacterEncoding=s;
}public void uploadFile(HttpServletRequest req) throws ServletException,IOException{
setCharacterEncoding(req.getCharacterEncoding());
setContentType(req.getContentType());
uploadFile(req.getInputStream());
}public void uploadFile(ServletInputStream servletinputstream) throws ServletException,IOException{
String s5=null;
String filename=null;
byte Linebyte[]=new byte[4096];
byte outLinebyte[]=new byte[4096];
int ai[]=new int[1];
int ai1[]=new int[1]; String line;
//得到文件名
while((line=readLine(Linebyte,ai,servletinputstream,CharacterEncoding))!=null){
int i=line.indexOf("filename=");
if(i>=0){
line=line.substring(i+10);
if((i=line.indexOf("\""))>0)
line=line.substring(0,i);
break;
}
}
filename=line;
if(filename!=null&&!filename.equals("\"")){
filename=getFileName(filename);
String sContentType=readLine(Linebyte,ai,servletinputstream,CharacterEncoding);
if(sContentType.indexOf("Content-Type")>=0)
readLine(Linebyte,ai,servletinputstream,CharacterEncoding); //File(String parent,String child)
//用路径名和文件名确定一个文件的实际位置,然后的到一个文件句柄 File file=new File(uploadDirectory,filename); //FileOutputStream(File file)
//用文件对象初始化一个输出流 FileOutputStream fileoutputstream=new FileOutputStream(file);
while((sContentType=readLine(Linebyte,ai,servletinputstream,CharacterEncoding))!=null){
if(sContentType.indexOf(ContentType)==0&&Linebyte[0]==45)
break;
if(s5!=null){
fileoutputstream.write(outLinebyte,0,ai1[0]);
fileoutputstream.flush();
}
s5=readLine(outLinebyte,ai1,servletinputstream,CharacterEncoding);
if(s5==null||s5.indexOf(ContentType)==0&&outLinebyte[0]==45)
break;
fileoutputstream.write(Linebyte,0,ai[0]);
fileoutputstream.flush();
}
byte byte0;
if(newline.length()==1)
byte0=2;
else
byte0=1;
if(s5!=null&&outLinebyte[0]!=45&&ai1[0]>newline.length()*byte0)
fileoutputstream.write(outLinebyte,0,ai1[0]-newline.length()*byte0);
if(sContentType!=null&&Linebyte[0]!=45&&ai[0]>newline.length()*byte0)
fileoutputstream.write(Linebyte,0,ai[0]-newline.length()*byte0); fileoutputstream.close();
}
}
private String readLine(byte Linebyte[],int ai[],ServletInputStream servletinputstream,String CharacterEncoding){
try{
//readLine(byte[] buffer,int offset,int length)
//从提交的数据中提取一行
ai[0]=servletinputstream.readLine(Linebyte,0,Linebyte.length);
if(ai[0]==-1)
return null;
}catch(IOException ex){
return null;
}
try{
if(CharacterEncoding==null){ //用缺省的编码方式把给定的byte数组转换为字符串 return new String(Linebyte,0,ai[0]);
}else{ //用给定的编码方式把给定的byte数组转换为字符串
//String(byte[] bytes,int offset,int length,String enc) return new String(Linebyte,0,ai[0],CharacterEncoding);
}
}catch(Exception ex){
return null;
}
}
public int readLine(byte[] buffer,int offset,int length) throws java.io.IOException //从POST来的数据中读一行
参数:
buffer-buffer to hold the line data
offset-offset into the buffer to start
length-maximum number of bytes to read
returns:
number of bytes read or -1 on the end of line
*/
{
方法体没写,请各位高手完成此处!感激不尽!
}}
import java.io.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletException;
public class upload{
private static String newline="\n";
private String uploadDirectory=".";
private String ContentType="";
private String CharacterEncoding="";private String getFileName(String s){
int i=s.lastIndexOf("\\");
if(i<0||i>=s.length()-1){
i=s.lastIndexOf("/");
if(i<0||i>s.length()-1)
return s;
}
return s.substring(i+1);
}public void setUploadDirectory(String s){
uploadDirectory=s;
}public void setContentType(String s){
ContentType=s;
int j;
if((j=ContentType.indexOf("boundary="))!=-1){
ContentType=ContentType.substring(j+9);
ContentType="--"+ContentType;
}
}public void setCharacterEncoding(String s){
CharacterEncoding=s;
}public void uploadFile(HttpServletRequest req) throws ServletException,IOException{
setCharacterEncoding(req.getCharacterEncoding());
setContentType(req.getContentType());
uploadFile(req.getInputStream());
}public void uploadFile(ServletInputStream servletinputstream) throws ServletException,IOException{
String s5=null;
String filename=null;
byte Linebyte[]=new byte[4096];
byte outLinebyte[]=new byte[4096];
int ai[]=new int[1];
int ai1[]=new int[1]; String line;
//得到文件名
while((line=readLine(Linebyte,ai,servletinputstream,CharacterEncoding))!=null){
int i=line.indexOf("filename=");
if(i>=0){
line=line.substring(i+10);
if((i=line.indexOf("\""))>0)
line=line.substring(0,i);
break;
}
}
filename=line;
if(filename!=null&&!filename.equals("\"")){
filename=getFileName(filename);
String sContentType=readLine(Linebyte,ai,servletinputstream,CharacterEncoding);
if(sContentType.indexOf("Content-Type")>=0)
readLine(Linebyte,ai,servletinputstream,CharacterEncoding); //File(String parent,String child)
//用路径名和文件名确定一个文件的实际位置,然后的到一个文件句柄 File file=new File(uploadDirectory,filename); //FileOutputStream(File file)
//用文件对象初始化一个输出流 FileOutputStream fileoutputstream=new FileOutputStream(file);
while((sContentType=readLine(Linebyte,ai,servletinputstream,CharacterEncoding))!=null){
if(sContentType.indexOf(ContentType)==0&&Linebyte[0]==45)
break;
if(s5!=null){
fileoutputstream.write(outLinebyte,0,ai1[0]);
fileoutputstream.flush();
}
s5=readLine(outLinebyte,ai1,servletinputstream,CharacterEncoding);
if(s5==null||s5.indexOf(ContentType)==0&&outLinebyte[0]==45)
break;
fileoutputstream.write(Linebyte,0,ai[0]);
fileoutputstream.flush();
}
byte byte0;
if(newline.length()==1)
byte0=2;
else
byte0=1;
if(s5!=null&&outLinebyte[0]!=45&&ai1[0]>newline.length()*byte0)
fileoutputstream.write(outLinebyte,0,ai1[0]-newline.length()*byte0);
if(sContentType!=null&&Linebyte[0]!=45&&ai[0]>newline.length()*byte0)
fileoutputstream.write(Linebyte,0,ai[0]-newline.length()*byte0); fileoutputstream.close();
}
}
private String readLine(byte Linebyte[],int ai[],ServletInputStream servletinputstream,String CharacterEncoding){
try{
//readLine(byte[] buffer,int offset,int length)
//从提交的数据中提取一行
ai[0]=servletinputstream.readLine(Linebyte,0,Linebyte.length);
if(ai[0]==-1)
return null;
}catch(IOException ex){
return null;
}
try{
if(CharacterEncoding==null){ //用缺省的编码方式把给定的byte数组转换为字符串 return new String(Linebyte,0,ai[0]);
}else{ //用给定的编码方式把给定的byte数组转换为字符串
//String(byte[] bytes,int offset,int length,String enc) return new String(Linebyte,0,ai[0],CharacterEncoding);
}
}catch(Exception ex){
return null;
}
}
public int readLine(byte[] buffer,int offset,int length) throws java.io.IOException //从POST来的数据中读一行
参数:
buffer-buffer to hold the line data
offset-offset into the buffer to start
length-maximum number of bytes to read
returns:
number of bytes read or -1 on the end of line
*/
{
方法体没写,请各位高手完成此处!感激不尽!
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货