首页/技术开发/内容

谈一下文件上传

技术开发2024-06-13 阅读()
在这里看到很多讨论文件上传的文章,觉得各有利敝,有些只限于上传文件,而不能同时取得文本字段值,尤其是上传多个文件比较少,现本人做这个上传文件的类最多可支持上传255个文件,同时可取得文本字段值,请各位高手指正.

文件上传类:MoqUploadBean.java
package net.jspcn.tool;

import java.io.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

/**

*

* Title: 文件上传类

* Description: 既能对文件进行上传,又能取得输入框的值,最多可同时上传255个文件

* Copyright: Copyright (c) 2002

* Company: Tekson

* @author 莫琼

* @version 1.0

*/

public class UploadBean {

private String[] sourceFile = new String[255]; //源文件名

private String[] suffix = new String[255]; //文件后缀名

private String canSuffix = ".gif.jpg.jpeg.png"; //可上传的文件后缀名

private String objectPath = "c:/"; //目标文件目录

private String[] objectFileName = new String[255]; //目标文件名

private ServletInputStream sis = null; //输入流

private String[] description = new String[255]; //描述状态

private long size = 100 * 1024; //限制大小

private int count = 0; //已传输文件数目

private byte[] b = new byte[4096]; //字节流存放数组

private boolean successful = true;

private Hashtable fields = new Hashtable();

public UploadBean() {

}

//设置上传文件的后缀名

public void setSuffix(String canSuffix) {

this.canSuffix = canSuffix;

}

//设置文件保存路径

public void setObjectPath(String objectPath) {

this.objectPath = objectPath;

}

//设置文件保存路径

public void setSize(long maxSize) {

this.size = maxSize;

}

//文件上传处理程序

public void setSourceFile(HttpServletRequest request) throws IOException {

sis = request.getInputStream();

int a = 0;

int k = 0;

String s = "";

while ( (a = sis.readLine(b, 0, b.length)) != -1) {

s = new String(b, 0, a);

if ( (k = s.indexOf("filename=\"")) != -1) {

// 取得文件数据

s = s.substring(k + 10);

k = s.indexOf("\"");

s = s.substring(0, k);

sourceFile[count] = s;

k = s.lastIndexOf(".");

suffix[count] = s.substring(k + 1);

if (canTransfer(count)) {

transferFile(count);

}

++count;

} else if ( (k = s.indexOf("name=\"")) != -1) {

// 普通表单输入元素,获取输入元素名字

String fieldName = s.substring(k+6, s.length()-3);

sis.readLine(b, 0, b.length);

StringBuffer fieldValue = new StringBuffer(b.length);

while ( (a = sis.readLine(b, 0, b.length)) != -1) {

s = new String(b, 0, a-2);

if ( (b[0] == 45) && (b[1] == 45) && (b[2] == 45) && (b[3] == 45) && (b[4] == 45)) {

break;

} else {

fieldValue.append(s);

}

}

fields.put(fieldName, fieldValue.toString());

}

if (!successful)

break;

}

}

//取得表单元素值

public String getFieldValue(String fieldName) {

if (fields == null (北联网教程,专业提供视频软件下载)

第1页  第2页  第3页  第4页  第5页 

……

相关阅读