You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by su...@apache.org on 2002/10/11 07:16:32 UTC
cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart FilePart.java
sullis 2002/10/10 22:16:32
Modified: httpclient/src/java/org/apache/commons/httpclient/methods
MultipartPostMethod.java
httpclient/src/java/org/apache/commons/httpclient/methods/multipart
FilePart.java
Log:
applied patch from "Mike" (becke -at- u.washington.edu)
Revision Changes Path
1.2 +9 -3 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/MultipartPostMethod.java
Index: MultipartPostMethod.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/MultipartPostMethod.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MultipartPostMethod.java 1 Oct 2002 19:13:41 -0000 1.1
+++ MultipartPostMethod.java 11 Oct 2002 05:16:32 -0000 1.2
@@ -148,6 +148,12 @@
parameters.add(param);
}
+ /**
+ * Adds another part to this post.
+ */
+ public void addPart( Part part ) {
+ parameters.add(part);
+ }
protected void addRequestHeaders(HttpState state, HttpConnection conn)
throws IOException, HttpException {
1.2 +55 -24 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/FilePart.java
Index: FilePart.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/FilePart.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FilePart.java 1 Oct 2002 19:13:41 -0000 1.1
+++ FilePart.java 11 Oct 2002 05:16:32 -0000 1.2
@@ -68,6 +68,7 @@
import java.io.OutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
+import java.security.InvalidParameterException;
/**
@@ -90,11 +91,13 @@
//TODO: make this configurable
static int MAX_BUFF_SIZE = 1 * 1024 * 1024; // 1 MiBs
+ private InputStream fileStream;
+ private String fileName;
+ private long fileLength;
+
private String name;
- private File file;
private int buff_size;
-
-
+
/**
* Constructor.
*
@@ -106,6 +109,9 @@
*/
public FilePart(String name, File file)
throws FileNotFoundException {
+
+ this( name, new FileInputStream(file), file.getName(), file.length() );
+
if (! file.isFile()) {
throw new FileNotFoundException("File is not a normal file.");
}
@@ -113,11 +119,35 @@
if (! file.canRead()) {
throw new FileNotFoundException("File is not readable.");
}
+
+ }
+ /**
+ * FilePart Constructor.
+ *
+ * @param name the name for this part
+ * @param fileInputStream an input stream for reading the file content
+ * @param fileName the name of the file
+ * @param fileLength the number of bytes contained in the file
+ */
+ public FilePart(
+ String name,
+ InputStream fileInputStream,
+ String fileName,
+ long fileLength
+ ) {
+
+ if ( fileLength < 0 ) {
+ throw new InvalidParameterException( "fileLength must be >= 0." );
+ }
+
this.name = name;
- this.file = file;
+ this.fileStream = fileInputStream;
+ this.fileName = fileName;
+ this.fileLength = fileLength;
+
}
-
+
protected void sendHeader(OutputStream out)
throws IOException {
super.sendHeader(out);
@@ -127,7 +157,7 @@
protected void sendFilename(OutputStream out)
throws IOException {
- String filename = "; filename=\"" + file.getName() + "\"";
+ String filename = "; filename=\"" + fileName + "\"";
out.write(filename.getBytes());
}
@@ -144,33 +174,34 @@
protected void sendData(OutputStream out)
throws IOException {
+
byte[] buff;
- if (lengthOfData() > MAX_BUFF_SIZE) {
+ if ( lengthOfData() == 0 ) {
+
+ // this file contains no data, so there is nothing to send.
+ // we don't want to create a zero length buffer as this will
+ // cause an infinite loop when reading.
+ return;
+
+ } else if (lengthOfData() > MAX_BUFF_SIZE) {
buff = new byte[MAX_BUFF_SIZE];
} else {
buff = new byte[(new Long(lengthOfData())).intValue()];
}
- InputStream in;
-
- try {
- in = new FileInputStream(file);
-
- int len;
+ int len;
- while ((len = in.read(buff)) != -1)
- {
- out.write(buff, 0, len);
- }
- } catch (FileNotFoundException e) {
- throw new IOException(e.toString());
+ while ((len = fileStream.read(buff)) != -1)
+ {
+ out.write(buff, 0, len);
}
+
}
protected long lengthOfData()
throws IOException {
- return file.length();
- }
+ return fileLength;
+ }
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>