在servlet中ServletInputStream的方法readLine怎么用?

twinkle_wy 2001-07-15 04:55:05
怎么用他实现读出一行字符?
...全文
182 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
package com.scce.servlet; import java.io.FileOutputStream; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class PhotoServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String type=request.getParameter("myType"); String encoding="UTF-8"; if(type.equals("upload")){ ServletInputStream sis=request.getInputStream(); int len=request.getContentLength(); int index=0; String tmp=null; boolean isFirst=true; String firstLine=null; int [] iindex=new int[1]; byte[] bytes=new byte[4096]; String filename=null; while((tmp=readLine(bytes,iindex,sis,encoding))!=null){ if(isFirst){ firstLine=tmp; isFirst=false; } index=tmp.indexOf("finename="); if(index!=-1){ String tailString = tmp.substring(index+10); if(tailString != null){ int ii=tailString.indexOf("\""); filename=tailString.substring(0,ii); } System.out.println(tmp); break; } filename=getName(filename); if(filename!=null){ filename="file.out1"; } String filepath="e:/"+filename; FileOutputStream fos=new FileOutputStream(filepath); String endFlag=firstLine.substring(0,firstLine.length()-2)+"--"+firstLine.substring(firstLine.length()-2); String contentType=readLine(bytes,iindex,sis,encoding); if(contentType!=null){ if(contentType.indexOf("Content-Type")==-1){ System.out.println(contentType); }else{ System.out.println("the head of file:"+readLine(bytes,iindex,sis,encoding)); } } boolean tt=false; int mark=0; byte[] backups=new byte[4096]; while((tmp=readLine(backups,iindex,sis,encoding))!=null){ if(endFlag.equals(tmp)){ if(mark>2){ fos.write(backups,0,mark-2); fos.flush(); } break; }else{ if(tt){ fos.write(backups,0,mark); fos.flush(); } mark=iindex[0]; for(int i=0;ireadLine(byte[] bytes, int[] index, ServletInputStream sis, String encoding) { try{ index[0]=sis.readLine(bytes, 0, bytes.length); if(index[0]<0){ return null; } }catch(IOException ex){ ex.printStackTrace(); } try{ if(encoding==null){ return new String(bytes,0,index[0]); }else{ return new String(bytes,0,index[0],encoding); } }catch(Exception e){ e.printStackTrace(); return null; } } }

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧