用jsp编写文件上载

2016-01-29 12:05 2 1 收藏

用jsp编写文件上载,用jsp编写文件上载

【 tulaoshi.com - Java 】

  如果你曾用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("rnrn");

temp=temp.substring(start1+4);
start2=temp.indexOf(";rn");
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

}

 

来源:https://www.tulaoshi.com/n/20160129/1484589.html

延伸阅读
标签: ASP
  现在“瘦客户”的观点已经是一个神话了,但随着电视或掌上型浏览器的繁荣,这一状况会有所改变。今天绝大多数的网络客户仍使用功能强大的PC,附着着大量的客户端存储器和客户端感兴趣的内容。在Internet协议下,将文件传递到中央服务器有一些方法可供选择,但基于WEB的文件上载比其它方法都要高级。下面来检验这一点。 一、HTTP...
标签: 电脑入门
首先,先来了解下JSP是什么,jsp是用java一种网页的脚本,正常情况可以用记事本,DREAMWEAVER等文本或网页设计工具打开进行编辑。不过这样只能看到源文件(就是代码),如果想知道运行结果就要安装相应的服务器软件。 jsp文件是无法直接用ie打开的等工具查看,只能放到服务器中客户端浏览。 jsp包装完后,要用Tomcate或者JBOSS来运行,用iE直接...
标签: Java JAVA基础
  作者:徐春金 JSP默认是以多线程方式执行的,这是JSP与ASP,PHP,PERL等脚本语言不一样的地方,也是它的优势之一,但如果不注意多线程中的同步问题,会使所写的JSP程序有难以发现的错误。下面以一个例子说明JSP中的多线程问题及解决方法。 一、JSP的中存在的多线程问题: 当客户端第一次请求某一个JSP文件时,服务端把该JSP编译成...
标签: Java JAVA基础
  <HTML <HEAD <TITLEJSP Bean Example</TITLE </HEAD <BODY <!-- Set the scripting language to java -- <%@ page language="java" % <!-- Instantiate the Counter bean with an id of "counter" -- <jsp:useBean id="counter" scope="session" class="...
标签: PHP
  <?php //多文件上载系统完整版 //功能强大,可任意控制上载文件数,是否覆盖 include("../include/common.inc"); $title = "多个文件的上载程序"; include("../include/header.inc"); //定义允许上载文件的数目 define("UPLOAD_NO", 10); echo("<p align='center'><font ...

经验教程

435

收藏

35
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部