Using buffered boundary stream in multipart parser. But currently not working
This commit is contained in:
parent
53dec4603c
commit
077963ae28
11 changed files with 198 additions and 152 deletions
|
|
@ -24,8 +24,11 @@
|
|||
|
||||
package zutil.net.http.multipart;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import zutil.io.IOUtil;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -34,14 +37,14 @@ import java.io.IOException;
|
|||
* @author Ziver
|
||||
*/
|
||||
public class MultipartFileField implements MultipartField{
|
||||
/** This is the temporary directory for the received files */
|
||||
private File tempDir;
|
||||
protected String filename;
|
||||
protected File file;
|
||||
|
||||
private String fieldname;
|
||||
private String filename;
|
||||
private InputStream in;
|
||||
|
||||
protected MultipartFileField() throws IOException {
|
||||
this.file = createTempFile();
|
||||
|
||||
protected MultipartFileField(Map<String,String> header, BufferedReader in) throws IOException {
|
||||
this.fieldname = header.get("fieldname");
|
||||
this.filename = header.get("filename");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -52,51 +55,28 @@ public class MultipartFileField implements MultipartField{
|
|||
}
|
||||
|
||||
/**
|
||||
* @return the specific file name for this field.
|
||||
* @return the field name
|
||||
*/
|
||||
public String getName(){
|
||||
return filename;
|
||||
return fieldname;
|
||||
}
|
||||
|
||||
public String getFilename(){
|
||||
return filename;
|
||||
}
|
||||
|
||||
public InputStream getInputStream(){
|
||||
return in;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the File class that points to the received file
|
||||
*/
|
||||
public File getFile(){
|
||||
return file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the received file.
|
||||
* Reads in all data and save it into the specified file
|
||||
*
|
||||
* @param newFile is the new location to move the file to
|
||||
* @return if the move was successful
|
||||
* @param file is the new file where the data will be stored
|
||||
*/
|
||||
public boolean moveFile(File newFile){
|
||||
boolean success = file.renameTo(newFile);
|
||||
if(success)
|
||||
file = newFile;
|
||||
return success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a temporary file in either the system
|
||||
* temporary folder or by the setTempDir() function
|
||||
*
|
||||
* @return the temporary file
|
||||
*/
|
||||
protected File createTempFile() throws IOException {
|
||||
if(tempDir != null)
|
||||
return File.createTempFile("upload", ".part", tempDir.getAbsoluteFile());
|
||||
else
|
||||
return File.createTempFile("upload", ".part");
|
||||
}
|
||||
|
||||
|
||||
public void setTempDir(File dir){
|
||||
if(!dir.isDirectory())
|
||||
throw new RuntimeException("\""+dir.getAbsolutePath()+"\" is not a directory!");
|
||||
if(!dir.canWrite())
|
||||
throw new RuntimeException("\""+dir.getAbsolutePath()+"\" is not writable!");
|
||||
tempDir = dir;
|
||||
public void saveToFile(File file) throws IOException {
|
||||
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
|
||||
IOUtil.copyStream(in, out);
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,17 +24,17 @@
|
|||
|
||||
package zutil.net.http.multipart;
|
||||
|
||||
import zutil.ProgressListener;
|
||||
import zutil.io.BufferedBoundaryInputStream;
|
||||
import zutil.io.IOUtil;
|
||||
import zutil.io.NullWriter;
|
||||
import zutil.log.LogUtil;
|
||||
import zutil.net.http.HttpHeader;
|
||||
import zutil.net.http.HttpHeaderParser;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
|
@ -57,23 +57,23 @@ public class MultipartParser implements Iterable<MultipartField>{
|
|||
/** The length of the HTTP Body */
|
||||
private long contentLength;
|
||||
/** This is the input stream */
|
||||
private BufferedReader in;
|
||||
private InputStream in;
|
||||
|
||||
private MultiPartIterator iterator;
|
||||
|
||||
|
||||
public MultipartParser(BufferedReader in, String delimiter, long length){
|
||||
public MultipartParser(InputStream in, String delimiter, long length){
|
||||
this.in = in;
|
||||
this.delimiter = delimiter;
|
||||
this.contentLength = length;
|
||||
}
|
||||
public MultipartParser(BufferedReader in, HttpHeader header){
|
||||
public MultipartParser(InputStream in, HttpHeader header){
|
||||
this(in,
|
||||
parseDelimiter(header.getHeader("Content-type")),
|
||||
Long.parseLong( header.getHeader("Content-Length")));
|
||||
}
|
||||
public MultipartParser(HttpServletRequest req) throws IOException {
|
||||
this(req.getReader(),
|
||||
this(req.getInputStream(),
|
||||
parseDelimiter(req.getHeader("Content-type")),
|
||||
req.getContentLength());
|
||||
}
|
||||
|
|
@ -101,56 +101,39 @@ public class MultipartParser implements Iterable<MultipartField>{
|
|||
|
||||
|
||||
protected class MultiPartIterator implements Iterator<MultipartField>{
|
||||
private String currentLine;
|
||||
private boolean endOfStream;
|
||||
private BufferedBoundaryInputStream boundaryIn;
|
||||
private BufferedReader buffIn;
|
||||
private HttpHeaderParser parser;
|
||||
|
||||
|
||||
protected MultiPartIterator(){
|
||||
this.endOfStream = false;
|
||||
this.parser = new HttpHeaderParser(in);
|
||||
this.boundaryIn = new BufferedBoundaryInputStream(in);
|
||||
this.buffIn = new BufferedReader(new InputStreamReader(boundaryIn));
|
||||
this.parser = new HttpHeaderParser(buffIn);
|
||||
this.parser.setReadStatusLine(false);
|
||||
|
||||
this.boundaryIn.setBoundary("--"+delimiter);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
try{
|
||||
findDelimiter();
|
||||
if (endOfStream)
|
||||
return false;
|
||||
return true;
|
||||
try {
|
||||
IOUtil.copyStream(buffIn, new NullWriter());
|
||||
return boundaryIn.hasNext();
|
||||
} catch (IOException e){
|
||||
logger.log(Level.SEVERE, null, e);
|
||||
endOfStream = true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private void findDelimiter() throws IOException {
|
||||
if (endOfStream || matchesDelimiter(currentLine))
|
||||
return;
|
||||
while ((currentLine=in.readLine()) != null) {
|
||||
if (matchesDelimiter(currentLine))
|
||||
break;
|
||||
}
|
||||
}
|
||||
private boolean matchesDelimiter(String match){
|
||||
if (match == null)
|
||||
return false;
|
||||
else if (match.equals("--" + delimiter + "--")) {
|
||||
endOfStream = true;
|
||||
return true;
|
||||
}
|
||||
else if (match.equals("--" + delimiter))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public MultipartField next() {
|
||||
if (!hasNext())
|
||||
return null;
|
||||
try {
|
||||
boundaryIn.next(); // Skip current boundary
|
||||
HttpHeader header = parser.read();
|
||||
String disposition = header.getHeader(HEADER_CONTENT_DISPOSITION);
|
||||
if (disposition != null){
|
||||
|
|
@ -158,16 +141,16 @@ public class MultipartParser implements Iterable<MultipartField>{
|
|||
HttpHeaderParser.parseHeaderValue(map, disposition);
|
||||
if (map.containsKey("form-data")){
|
||||
if (map.containsKey("filename")){
|
||||
MultipartFileField field = new MultipartFileField();
|
||||
MultipartFileField field = new MultipartFileField(map, buffIn);
|
||||
return field;
|
||||
}
|
||||
else{
|
||||
MultipartStringField field = new MultipartStringField(map.get("name"));
|
||||
MultipartStringField field = new MultipartStringField(map, buffIn);
|
||||
return field;
|
||||
}
|
||||
}
|
||||
else {
|
||||
logger.warning("Only form-data is supported");
|
||||
logger.warning("Only multipart form-data is supported");
|
||||
return this.next(); // find next field
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,12 @@
|
|||
package zutil.net.http.multipart;
|
||||
|
||||
|
||||
import java.util.HashMap;
|
||||
import zutil.io.IOUtil;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* Created by Ziver on 2016-07-06.
|
||||
|
|
@ -10,8 +15,9 @@ public class MultipartStringField implements MultipartField {
|
|||
private String name;
|
||||
private String value;
|
||||
|
||||
protected MultipartStringField(String name){
|
||||
this.name = name;
|
||||
protected MultipartStringField(Map<String,String> header, BufferedReader in) throws IOException {
|
||||
this.name = header.get("name");
|
||||
value = IOUtil.getContentAsString(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue