aspose file tools*
The moose likes JSF and the fly likes Upload more than 1 MB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Upload more than 1 MB" Watch "Upload more than 1 MB" New topic
Author

Upload more than 1 MB

Vinicius Monteiro
Greenhorn

Joined: Sep 19, 2009
Posts: 4
Hi,

I have been using ExtensionsFilter in one of the application for uploading files under 20MB. ExtensionsFilter' is set in my web.xml file with 25m i.e. MB of maxFileSize.
It's working properly if i upload files under 1MB but for the file over 1MB when i hit upload button, browser gives up right away and does nothing. It does not even go to server. I mean none of the perperty is being set.

Is there anthing outside the scope of the application that's cause of this problem, I mean e.g. anything related to tomcat server settings? Please anyone guide me on this?

Thanks in advance.

Regards,
Vinicius
Rajneesh Kumar Rajput
Ranch Hand

Joined: Feb 25, 2009
Posts: 39
try setting this variable too...


This is from my faces 1.2.6 if you are using old version, variable name will be different,
Vinicius Monteiro
Greenhorn

Joined: Sep 19, 2009
Posts: 4
I had set this already. i encountered the problem...i was settin the temp folder in the web.xml, but the folder hadn't been created yet.
Rajneesh Kumar Rajput
Ranch Hand

Joined: Feb 25, 2009
Posts: 39
just a guess, did you server has temp directory already created in its root?
if not please create temp folder manually, and restart server.

if it does not have, it will not create it automatically but will not upload big files.
Vinicius Monteiro
Greenhorn

Joined: Sep 19, 2009
Posts: 4
yep, i already post the solution. the temp directory hadnt been created yet, that was the problem, i thought it would be created automatically.

Problem Solved.

Thanks.
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
Hi Vinicius,

Do you have a sample application that you can share with me for uploading a file?
I followed the link:

http://onjava.com/pub/a/onjava/2005/07/13/jsfupload.html?page=4

and I am having problems. I have posted here:

http://www.coderanch.com/t/464707/JSF/java/deploying-jsf-application-tomcat

Thanks,

Sonia
Vinicius Monteiro
Greenhorn

Joined: Sep 19, 2009
Posts: 4
of course i do, here it is:

web.xml
<filter>
<filter-name>extensionsFilter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
<init-param>
<param-name>uploadMaxSize</param-name>
<param-value>100m</param-value>
</init-param>
<init-param>
<param-name>uploadMaxFileSize</param-name>
<param-value>100m</param-value>
</init-param>
<init-param>
<description>
Set the threshold size - files below this limit are stored
in memory, files above this limit are stored on disk.
Format: 10 - 10 bytes
10k - 10 KB
10m - 10 MB
1g - 1 GB
</description>
<param-name>uploadThresholdSize</param-name>
<param-value>100k</param-value>
</init-param>
<init-param>
<param-name>uploadRepositoryPath</param-name>
<param-value>/temp</param-value>
</init-param>
</filter>

JSP

<t:inputFileUpload size="50" id="myFileId"
value="#{mensagemMB.uploadedFile}"
storage="file"
/>

ManagerBean

private UploadedFile uploadedFile;

public String upload(){

//The path can be:
String path = "C:/uploads";
//Or relative to your application:
FacesContext ctx = FacesContext.getCurrentInstance();
ServletContext servletContext = (ServletContext) ctx.getExternalContext().getContext();
String realPath = servletContext.getRealPath("/uploads");

String uploadedFileName = this.trimFilePath(uploadedFile.getName());
File file = FileUtil.uniqueFile(new File(path + "/"), uploadedFileName);
FileUtil.write(file, uploadedFile.getInputStream());

return null;

}

Aux Methods

/**
* Trim the eventual file path from the given file name. Anything before the last occurred "/"
* and "\" will be trimmed, including the slash.
* @param fileName The file name to trim the file path from.
* @return The file name with the file path trimmed.
*/

public static String trimFilePath(String fileName) {
return fileName
.substring(fileName.lastIndexOf("/") + 1)
.substring(fileName.lastIndexOf("\\") + 1);
}

/**
* Generate unique file based on the given path and name. If the file exists, then it will
* add "[i]" to the file name as long as the file exists. The value of i can be between
* 0 and 2147483647 (the value of Integer.MAX_VALUE).
* @param filePath The path of the unique file.
* @param fileName The name of the unique file.
* @return The unique file.
* @throws IOException If unique file cannot be generated, this can be caused if all file
* names are already in use. You may consider another filename instead.
*/

public static File uniqueFile(File filePath, String fileName) throws IOException {
File file = new File(filePath, fileName);

if (file.exists()) {

// Split filename and add braces, e.g. "name.ext" --> "name[", "].ext".
String prefix;
String suffix;
int dotIndex = fileName.lastIndexOf(".");

if (dotIndex > -1) {
prefix = fileName.substring(0, dotIndex) + "[";
suffix = "]" + fileName.substring(dotIndex);
} else {
prefix = fileName + "[";
suffix = "]";
}

int count = 0;

// Add counter to filename as long as file exists.
while (file.exists()) {
if (count < 0) { // int++ restarts at -2147483648 after 2147483647.
throw new IOException("No unique filename available for " + fileName
+ " in path " + filePath.getPath() + ".");
}

// Glue counter between prefix and suffix, e.g. "name[" + count + "].ext".
file = new File(filePath, prefix + (count++) + suffix);
}
}

return file;
}

/**
* Write byte inputstream to file. If file already exists, it will be overwritten.It's highly
* recommended to feed the inputstream as BufferedInputStream or ByteArrayInputStream as those
* are been automatically buffered.
* @param file The file where the given byte inputstream have to be written to.
* @param input The byte inputstream which have to be written to the given file.
* @throws IOException If writing file fails.
*/

public static void write(File file, InputStream input) throws IOException {
write(file, input, false);
}

public UploadedFile getUploadedFile() {
return uploadedFile;
}

public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
Thanks,

Sonia
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
Vinicius,

You might know the answer to this:

In my jsp page I have:
<h:commandButton value="Submit" action="#{myBean.submit}" />

I am getting an error in the page. It says the method submit must return a String. Even when
I change it to return a String. I still have the error.

Know anything about this?

THanks,

Sonia
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Upload more than 1 MB