A full upload bean---reprint
Last Update:2017-02-28
Source: Internet
Author: User
Upload Package Popeyelin;
import java.io.*;
import Javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
public class Transfer_multi {
public string[] sourcefile = new string[255];//source filename
public String ObjectPath = "c:/";//destination file directory
public string[] suffix = new string[255];//file suffix name
public string[] Objectfilename = new string[255];//destination file name
public ServletInputStream sis = null;//input stream
public string[] Description = new string[255];//description status
public long size = 100*1024;//Limit size
private int count = 0;//number of transferred files
private byte[] b = new byte[4096];//byte stream storage array
Private Boolean successful = true;
public void Setsourcefile (HttpServletRequest request) throws java.io.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);
System.out.println (Suffix[count]);
if (Cantransfer (count)) Transferfile (count);
}
if (!successful) break;
}
}
public int GetCount () {
return count;
}
public string[] Getsourcefile () {
return sourcefile;
}
public void Setobjectpath (String objectpath) {
This.objectpath = ObjectPath;
}
public String Getobjectpath () {
return objectpath;
}
private Boolean cantransfer (int i) {
Suffix[i] = Suffix[i].tolowercase ();
//This is my use to send pictures, you can change the suffix name or not this condition
if (Sourcefile[i].equals ("") | | (!suffix[i].equals ("GIF") &&!suffix[i].equals ("JPG") &&!suffix[i].equals ("JPEG")) {description[i]= ' ERR 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;//Indicates the number of bytes that have been transmitted
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 the transfered successfully.";
++count;
Out.close ();
if (!successful) {
Sis.close ();
File TMP = new file (objectpath+objectfilename[count-1]);
Tmp.delete ();
}
}
catch (IOException IoE) {
description[i]=ioe.tostring ();
}
}
public Transfer_multi () {
//Can build the server upload directory inside the builder, or build it yourself in the JavaBean call
Setobjectpath ("/home/www/jspvhost4/web/popeyelin/images/");
}
}