用JSP写出FileUpload上传小程序

来源:互联网 发布:windows重装系统 编辑:程序博客网 时间:2024/05/06 05:58
Reference: http://www.7880.com/Info/Article-42b729a0.html

新的是:

void write(Java.io.File file)
A convenience method to write an uploaded item to disk.

而这里的是String!!!

Constructor Summary
File(File parent, String child)
Creates a new File instance from a parent abstract pathname and a child pathname string.
File(String pathname)
Creates a new File instance by converting the given pathname string into an abstract pathname.
File(String parent, String child)
Creates a new File instance from a parent pathname string and a child pathname string.
File(URI uri)
Creates a new File instance by converting the given file: URI into an abstract pathname.
我修改了这句为两句:

File writeFile=new File("F://public",name);
fi.write(writeFile);
这样这个上传程序终于可以工作了!(一会有源代码,上传一个文件的)
还有这里的"" 都是""(输入法问题),如果粘贴过来要一个个修改,不知道怎么回事情,网上的代码总是有一点让人难以琢磨的错误而不能运行!所以网上的东西只能参考理解之后再自己写!

其它过程:

1.upload
B 端的上传
1)
 upload目录:
<form action="getUpload.JSP" enctype="multipart/form-data" method="POST">
这里oc4j 的目录和 tomcat 的目录可是服务器硬盘上真实存在的任意目录!
2)upload method.
请输入要上传的文件:<input type="FILE" name="file"/>
2.get
S端读取:
1).request.getInputStream 进行分析
public ServletInputStream getInputStream() throws java.io.IOException

2).Jakarta 通用库

3.download

 

temp sql:
create table upload(
name varchar2(16) primary key not null,
content clob );
commit;
server 端接受文件上传, 下载commons-fileupload-1.0, http://jakarta.apache.org/commons/fileupload/
说明:
Commons是 Apache开放源代码组织中的一个Java子项目,该项目主要涉及一些开发中常用的模块,例如文件上传、命令行处理、数据库连接池、 XML配置文件处理等。这些项目集合了来自世界各地软件工程师的心血,其性能、稳定性等方面都经受得住实际应用的考验。有效地利用这些项目将会给开发带来 显而易见的效果。Fileupload就是其中用来处理HTTP文件上传的子项目。本文主要介绍如何使用Fileupload来处理浏览器提交到服务器的 文件信息。

 

 

PS:一般下载的*.jar 文件都是拷贝到Tomcat 5.5/common/lib里面

 

另外, 由于Fileupload子项目同时要用到另外一个项目commons-Beanutils,所以必须下载Beanutils,并将解压后的文件commons-beanutils.jar拷贝到{$TOMCAT}/common/lib目录下

 

 

Reference: http://www.7880.com/Info/Article-42b729a0.html


我写的参考源代码:

//inputupload.jsp

<%@ page contentType="text/html;charset=Big5"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Big5">
<title>inputUpload</title>
</head>
<body>
<%
request.setCharacterEncoding("big5");
%>
<form action="getUpload.jsp" enctype="multipart/form-data" method="POST" >
請輸入要上傳的文件:<input type="FILE" name="file"/>
<input type="submit" value="確定上傳"/>
</form>

</body>
</html>

 

//getUpload.jsp

<%@ page contentType="text/html;charset=GBK"%>

<%@ page import="java.util.*"%>

<%@ page import="java.io.*"%>

<%@ page import="org.apache.commons.fileupload.*"%>

<%@ page import="org.apache.commons.beanutils.*"%>

 

getUpload.jsp

<%

DiskFileUpload dfu = new DiskFileUpload();

// 设置允许用户上传文件大小,单位:字节

dfu.setSizeMax(1000000);

// maximum size that will be stored in memory?

// 设置最多只允许在内存中存储的数据,单位:字节

dfu.setSizeThreshold(4096);

// 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录

dfu.setRepositoryPath("f://public");

//开始读取上传信息

try{

List fileItems = dfu.parseRequest(request);

%>

<%

// 依次处理每个上传的文件

Iterator i = fileItems.iterator();

String name =null;

long size=0;

while (i.hasNext())

{

FileItem fi = (FileItem) i.next();

//忽略其他不是文件域的所有表单信息

if (!fi.isFormField()) {

name = fi.getName();

size = fi.getSize();

if((name==null||name.equals("")) && size==0)

continue; }

name=fi.getName();

size=fi.getSize();

name = name.replace(':','_');

name = name.replace('//','_');

File writeFile=new File("F://public",name);

fi.write(writeFile);

}

}catch(FileUploadException fue)

{ fue.printStackTrace();}

%>

 



***************

经验:

1. API 非常重要.比任何参考书都重要!

2. 任何东西只能做为参考,只有自己写出来的才是自己的!

3. 遇到什么困难,只有专注和坚持不懈,毕竟已经我们站在别人的肩膀上了,别人开始走的路比我们更艰难,所以我们没有攻不下来的!

我的参考源码,记住,只能给你参考,也许程序里有bug,欢迎指教:)

有问题可以一起讨论:)

 

*************