use of net.jforum.util.legacy.commons.fileupload.FileUploadException in project jforum2 by rafaelsteil.
the class WebRequestContext method handleMultipart.
/**
* @param superRequest HttpServletRequest
* @param encoding String
* @throws UnsupportedEncodingException
*/
private void handleMultipart(HttpServletRequest superRequest, String encoding) throws UnsupportedEncodingException {
String tmpPath = new StringBuffer(256).append(SystemGlobals.getApplicationPath()).append('/').append(SystemGlobals.getValue(ConfigKeys.TMP_DIR)).toString();
File tmpDir = new File(tmpPath);
boolean success = false;
try {
if (!tmpDir.exists()) {
tmpDir.mkdirs();
success = true;
}
} catch (Exception e) {
// We won't log it because the directory
// creation failed for some reason - a SecurityException
// or something else. We don't care about it, as the
// code below tries to use java.io.tmpdir
}
if (!success) {
tmpPath = System.getProperty("java.io.tmpdir");
tmpDir = new File(tmpPath);
}
ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory(100 * 1024, tmpDir));
upload.setHeaderEncoding(encoding);
try {
List items = upload.parseRequest(superRequest);
for (Iterator iter = items.iterator(); iter.hasNext(); ) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
this.addParameter(item.getFieldName(), item.getString(encoding));
} else {
if (item.getSize() > 0) {
// We really don't want to call addParameter(), as
// there should not be possible to have multiple
// values for a InputStream data
this.query.put(item.getFieldName(), item);
}
}
}
} catch (FileUploadException e) {
throw new MultipartHandlingException("Error while processing multipart content: " + e);
}
}
use of net.jforum.util.legacy.commons.fileupload.FileUploadException in project jforum2 by rafaelsteil.
the class DiskFileItem method write.
/**
* A convenience method to write an uploaded item to disk. The client code
* is not concerned with whether or not the item is stored in memory, or on
* disk in a temporary location. They just want to write the uploaded item
* to a file.
* <p>
* This implementation first attempts to rename the uploaded item to the
* specified destination file, if the item was originally written to disk.
* Otherwise, the data will be copied to the specified file.
* <p>
* This method is only guaranteed to work <em>once</em>, the first time it
* is invoked for a particular item. This is because, in the event that the
* method renames a temporary file, that file will no longer be available
* to copy or rename again at a later time.
*
* @param file The <code>File</code> into which the uploaded item should
* be stored.
*
* @exception Exception if an error occurs.
*/
public void write(File file) throws Exception {
if (isInMemory()) {
FileOutputStream fout = null;
try {
fout = new FileOutputStream(file);
fout.write(get());
} finally {
if (fout != null) {
fout.close();
}
}
} else {
File outputFile = getStoreLocation();
if (outputFile != null) {
/*
* The uploaded file is being stored on disk
* in a temporary location so move it to the
* desired file.
*/
if (!outputFile.renameTo(file)) {
BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(outputFile));
out = new BufferedOutputStream(new FileOutputStream(file));
byte[] bytes = new byte[WRITE_BUFFER_SIZE];
int s = 0;
while ((s = in.read(bytes)) != -1) {
out.write(bytes, 0, s);
}
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// ignore
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
// ignore
}
}
}
}
} else {
/*
* For whatever reason we cannot write the
* file to disk.
*/
throw new FileUploadException("Cannot write uploaded file to disk!");
}
}
}
Aggregations