| http://www.zk168.com.cn 招考学习网 2006-4-11 1:48:18 |
|
| -----------------------------------------------------------[交流]-[打印]-[发送]-[收藏]-- |
|
| 如果你曾用VB编写文件上传的组件的话,那么用JAVA编写文件上传的JAVABEAN十分容易。 下面的例子只是一个简版 package yuanyifileup; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.PageContext;
public class yuanyifileup { private ServletRequest request; private ServletResponse response; private ServletConfig config; ServletInputStream DATA; int FormSize; File f1; FileOutputStream os; DataInputStream is; String filename; byte[] b; byte t; boolean flag=false; public yuanyifileup() { } public void initialize(ServletConfig config,HttpServletRequest request,HttpServletResponse response) throws IOException { this.request=request; this.response=response; this.config=config; DATA = request.getInputStream(); FormSize=request.getContentLength(); } public void initialize(PageContext pageContext) throws IOException { request=pageContext.getRequest(); response=pageContext.getResponse(); config=pageContext.getServletConfig(); DATA = request.getInputStream(); FormSize=request.getContentLength(); } public boolean setFilename(String s) { try { File f1=new File(s); os=new FileOutputStream(f1); } catch(IOException e) {return(false);} return(true); } public void getByte() { int i=0; try { is=new DataInputStream(DATA); b=new byte[FormSize];
while (true) { try { t=is.readByte(); b[i]=t; i++; } catch(EOFException e) { break;} } is.close();} catch(IOException e) {} }
public boolean save() { int i=0,start1=0,start2=0; String temp=""; if (!flag) { getByte(); flag=true; } try { temp=new String(b,"ISO8859_1"); } catch(UnsupportedEncodingException e) {return(false);}
start1=temp.indexOf("image/"); temp=temp.substring(start1);
start1=temp.indexOf("\r\n\r\n");
temp=temp.substring(start1+4); start2=temp.indexOf(";\r\n"); if (start2!=-1) { temp=temp.substring(0,start2); } try { byte[] img=temp.getBytes("ISO8859_1"); for (i=0;i<img.length;i++) { os.write(img[i]); } os.close(); } catch(IOException e) {return(false);}
return(true);
} 如果有不明白的发E-Mail:yymailbox@263.net.Bye
}
|
| -----------------------------------------------------------[交流]-[打印]-[发送]-[收藏]-- |
友情提醒:
1.库中的资料大都来自互联网、网友上传、各类书籍,在录入的过程中难免会出现错误,恳请网 友来信指正!
2.如果网友在本库中未能找到所需要的材料,请登陆到我们的论坛《招考学习网》版块!
3.考友想加入招考学习网的编辑部,请发信到XueXiWang#Gmail.com(#改为@)附带个人简历
4.如需转载请注明出处及作者,谢谢合作!
5.如果您有更好的建议或意见请EMAIL:XueXiWang#Gmail.com (#改为@)
6.凡标题中有注有“[NO]”字样均不含答案且答案整理中.
7.如本库中转载文章涉及版权等问题,请相关网站或作者在两周内发邮件通知(EMAIL: XueXiWang#Gmail.com (#改为@))我们,我们接到通知后立即删除该文章及链接! |
|