⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uploadbean.java

📁 这是我老师布置一个学习任务:保险管理系统
💻 JAVA
字号:

package net.jspcn.tool;

import java.io.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;



public class UploadBean {

private String[] sourceFile = new String[1000]; 

private String[] suffix = new String[1000];

private String canSuffix = ".gif.jpg.jpeg.png"; 

private String objectPath = "c:/"; 

private String[] objectFileName = new String[1000]; 

private ServletInputStream sis = null;

private String[] description = new String[1000]; 

private long size = 1000 * 1024; 

private int count = 0; 

private byte[] b = new byte[40960]; 

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) {
//while ( (a = sis.readLine(0, 0, b.length)) != -1) {

s = new String(b, 0, a);
System.out.println(s);
if ( (k = s.indexOf("filename=\"")) != -1) {
s = s.substring(k + 10);
System.out.println("s = s.substring(k + 10)");
System.out.println("k:"+k+"\ns:"+s+"\na:"+a);
k = s.indexOf("\"");
System.out.println("k= s.indexOf(\\\")\n"+k);
s = s.substring(0, k);
System.out.println("s = s.substring(0, k)\n"+s);

sourceFile[count] = s;
System.out.println("sourceFile[count] = s\n"+sourceFile[count]);
k = s.lastIndexOf(".");
System.out.println("k = s.lastIndexOf(\".\")\n"+k);
suffix[count] = s.substring(k + 1);

if (canTransfer(count)) {

transferFile(count);

}

++count;
System.out.println("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 || fieldName == null) {

return null;

}

return (String) fields.get(fieldName);

}

//

public int getCount() {

return count;

}

//

public String getObjectPath() {

return objectPath;

}

//

public String[] getSourceFile() {

return sourceFile;

}

//

public String[] getObjectFileName() {

return objectFileName;

}



public String[] getDescription() {

return description;

}



private boolean canTransfer(int i) {

suffix[i] = suffix[i].toLowerCase();


if (sourceFile[i].equals("") || (!(canSuffix.indexOf("."+suffix[i])>=0))) {

description[i] = "ERR: File suffix is wrong.";

return false;

}

else {

return true;

}

}



private void transferFile(int i) {

String x = Long.toString(new java.util.Date().getTime());

try {

objectFileName[i] = x + "." + suffix[i];

FileOutputStream out = new FileOutputStream(objectPath + objectFileName[i]);

int a = 0;

int k = 0;

long hastransfered = 0; 

String s = "";

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

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

if ( (k = s.indexOf("Content-Type:")) != -1) {

break;

}

}

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

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

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

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

break;

}

out.write(b, 0, a);

hastransfered += a;

if (hastransfered >= size) {

description[count] = "ERR: The file " + sourceFile[count] +

" is too large to transfer. The whole process is interrupted.";

successful = false;

break;

}

}

if (successful) {

description[count] = "Right: The file " + sourceFile[count] +

" has been transfered successfully.";

}

out.close();

if (!successful) {

sis.close();

File tmp = new File(objectPath + objectFileName[count]);

tmp.delete();

}

}

catch (IOException ioe) {

description[i] = ioe.toString();

}

}

public static void main(String[] args) {

System.out.println("Test OK");

}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -