国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

一個文件上傳JAVABEAN

2019-11-18 11:02:57
字體:
來源:轉載
供稿:網友

 


package  com.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("http://");  
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)  
//Creates  a  new  File  instance  from  a  parent  pathname  string  
//and  a  child  pathname  string.  
File  file  =  new  File(uploadDirectory,  filename);  

//FileOutputStream(File  file)  
//Creates  a  file  output  stream  to  write  to  the  file  represented  
//by  the  specified  File  object.  
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){  
//write(byte[]  b,  int  off,  int  len)  
//Writes  len  bytes  from  the  specified  byte  array  starting  
//at  offset  off  to  this  file  output  stream.  
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){  



上一篇:多線程學習初步

下一篇:JavaBean入門

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 呼玛县| 铁岭市| 砚山县| 海阳市| 阿勒泰市| 邛崃市| 施甸县| 吴江市| 乐至县| 肥西县| 民丰县| 滦平县| 棋牌| 子长县| 福海县| 宜兰市| 丘北县| 普宁市| 铜梁县| 安泽县| 肇庆市| 肇州县| 钟祥市| 通江县| 广南县| 德格县| 新泰市| 宜黄县| 米林县| 聂荣县| 江门市| 新化县| 封丘县| 淮安市| 固镇县| 资溪县| 锦州市| 尤溪县| 烟台市| 无锡市| 二连浩特市|