File upload by JSP (轉)

worldblog發表於2007-12-11
File upload by JSP (轉)[@more@]

In forum, a lot of people are looking for another way to file by ,other than SmartUpload,which is a free bean from jspsmart.com.

Although its functionality is powerful, at least called "enough", the code is invisible to us,which is a little pity of this bean.

A few days ago,I got an article talking about the upload things on on.com.

and the author provd a piece of code to implement upload feature.

Click   to enjoy it!

However, there is also a flaw in his code, that's it only supports text plain file but binary format.In fact, most of files to upload are in binary format,aren't they?

But don't worry, I have made some modification on the original sourcecode to enable it to support all file formats.

Now I paste the whole source code below,

hopefully it can be helpful to those who are interested in this problem.

BTW: My E is , welcome to send mail and discuss with me on any Java technical issues.

Code:

import javax..http.HttpServletRequest;
import javax.servlet.ServletInputStream;
import java.util.Dictionary;
import java.util.Hashtable;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class FileUploaean{
  private String savePath, filepath, filename, contentType;
  private Dictionary fields;
  //Begin Rayman's modification
  private static final int BUF_SIZE = 256;
  //End Rayman's modification

  public String getFilename() {
  return filename;
  }

  public String getFilepath() {
  return filepath;
  }

  public void setSavePath(String savePath) {
  this.savePath = savePath;
  }

  public String getContentType() {
  return contentType;
  }

  public String getFieldValue(String fieldName) {
  if (fields == null ¦¦ fieldName == null)
  {
  //System.out.println(fieldName);
  //System.out.println("if l");
  return null;
  }
  //System.out.println(fieldName);
  //System.out.println(fields.get("company"));
  return (String)fields.get(fieldName);
  }

  private void setFilename(String s) {

  if (s==null)
  return;

  int p= s.indexOf("filename="");
  if (pos != -1) {
  filepath = s.substring(pos+10, s.length()-1);
  // browsers include the full path on the client
  // But / and Mac browsers only send the filename
  // test if this is from a Windows browser
  pos = filepath.lastIndexOf("");
  if (pos != -1)
  filename = filepath.substring(pos + 1);
  else
  filename = filepath;
  }
  }

  private void setContentType(String s){
  if (s==null)
  return;

  int pos = s.indexOf(": ");
  if (pos != -1)
  contentType = s.substring(pos+2, s.length());
  }

 

  public void doUpload(HttpServletRequest request) throws IOException{

  ServletInputStream in = request.getInputStream();


  byte[] line = new byte[BUF_SIZE];
  int i = in.readLine(line, 0, 128);
  if (i < 3)
  return;
  int boundaryLength = i - 2;

  String boundary = new String(line, 0, boundaryLength); //-2 discards the newline character
  fields = new Hashtable();

  while (i != -1) {
  String newLine = new String(line, 0, i);
  if (newLine.startsWith("Content-Disposition: form-data; name="")) {
  if (newLine.indexOf("filename="") != -1) {
  setFilename(new String(line, 0, i-2));
  if (filename==null)
  return;
  //this is the file content
  i = in.readLine(line, 0, 128);
  setContentType(new String(line, 0, i-2));
  i = in.readLine(line, 0, 128);
 
  //Begin Rayman's modification
  FileOutputStream pw = new FileOutputStream((savePath==null? "" : savePath) + filename);
  boolean done = false;
  int k = 0;
  byte c1 = 0x0d;
  byte c2 = 0x0a;

  byte [] boundarybytes = boundary.getBytes();
  byte [] magickey = new byte[boundarybytes.length + 2];
  magickey[0] = c1;
  magickey[1] = c2;
  for(int t = 0;t  {
  magickey[2+t] = boundarybytes[t];
  }
 
  byte [] buf = new byte[magickey.length];
  int count = 0;
  byte c = -1;
  boolean pause = false;
  while (true) {
  // the problem is the last line of the file content
  // contains the new line character.
  // So, we need to check if the current line is
  // the last line
  if(!pause)
  {
  byte [] one = new byte[1];
  if(in.read(one)==-1) break;
  c = one[0];
  count ++;
  }
  else
  {
  pause = false;
  }

  if(c==magickey[k])
  {
  System.out.print((int)c + " " + (char)c + " ");
  if(k == magickey.length-1)
  {
  //It's last line.
  System.out.println("Last line!" + " count==" + count);
  break;
  }
  buf[k] = (byte)c;
  k++;
  }
  else
  {
  if(k>0)
  {

  pw.write(buf,0,k);

  }
  if(c==magickey[0])
  {
  pause = true;
  }
  else
  {
  pw.write(c);
  }
  k=0;
  }
  }
  pw.close();
   //End Rayman's modification

  }
  else {
  //this is a field
  // get the field name
  int pos = newLine.indexOf("name="");
  String fieldName = newLine.substring(pos+6, newLine.length()-3);
  //System.out.println("fieldName:" + fieldName);
  // blank line
  i = in.readLine(line, 0, 128);
  i = in.readLine(line, 0, 128);
  newLine = new String(line, 0, i);
  StringBuffer fieldValue = new StringBuffer(128);
  while (i != -1 && !newLine.startsWith(boundary)) {
  // The last line of the field
  // contains the new line character.
  // So, we need to check if the current line is
  // the last line.
  i = in.readLine(line, 0, 128);
  if ((i==boundaryLength+2 ¦¦ i==boundaryLength+4) // + 4 is eof
  && (new String(line, 0, i).startsWith(boundary)))
  fieldValue.append(newLine.substring(0, newLine.length()-2));
  else
  fieldValue.append(newLine);
  newLine = new String(line, 0, i);
  }
  //System.out.println("fieldValue:" + fieldValue.toString());
  fields.put(fieldName, fieldValue.toString());
  }
  }
  i = in.readLine(line, 0, 128);

  } // end while
  }

  public FileUploadBean()
  {
  }
}

 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-991215/,如需轉載,請註明出處,否則將追究法律責任。

相關文章