• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Struts 2 Fileupload problem

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am stuck with file upload i have searched many forums and
websites they all seem to give almost the same code. But none of them
worked out for me. I have tried my best to upload a file but every
time I get a message. There are no error messages though

INFO: Removing file upload \tmp
\upload__37055a77_11ac9568d10__7ffe_00000033.tmp


and file does not gets uploaded. only a folder named tmp gets created
in root directory of my web server. Can someone please give me
location of a working example.

In
http://www.roseindia.net/struts/struts2/struts-2-file-upload.shtml

they have mentioned that
"The File Upload Interceptor actually deletes the the upload, once the
action is executed"

If this is true then why does it behaves like this. Please help I am
kind of stuck with this since last 2 weeks
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this,

Form
-----
String FormFile fileToUpload;

JSP
----
<html:form method="post" action="your actionpath" enctype="multipart/form-data" >

<html:file name="SomeForm" property="fileToUpload" onchange="document.forms[0].submit();/>

This is the struts way to upload a file.
Let me know if this works.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sharad,

Struts delivers to your action class a java.io.File containing the uploaded data. Once the execute method of your action has executed, Struts deletes this temporary file. That means it's up to you to do something with the uploaded file in the execute method of your Action class: copy it to a file in a permanent location, copy it to a BLOB in a database, display its contents, etc. If you do nothing, the file just gets deleted.

Jaffer,

Sharad is trying to do a Struts 2 file upload, so your method relying on Struts 1 tags would not work in this case.
 
Sharad Sharma
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks merril.. i never thought of the same.. but in the code as you can see there is only

can you please help me out how to do the same.. i m not using any database.. just taking file from one location and putting in another.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is a link that shows you how to copy data from one file to another.

http://exampledepot.com/egs/java.io/CopyFile.html
 
Sharad Sharma
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot that helped..
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this,

Form
-----
String FormFile fileToUpload;

JSP
----
<html:form method="post" action="your actionpath" enctype="multipart/form-data" >

<html:file name="SomeForm" property="fileToUpload" onchange="document.forms[0].submit();/>

This is the struts way to upload a file.
I think it should work.
[ August 26, 2008: Message edited by: David O'Meara ]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suresh,

What you posted is the Struts 1 way to upload a file. Sharad is using Struts 2, so it won't work in this case.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am trying file upload in struts2.18. It is not working i am always getting null in the action class. here is the code block


--
struts.xml


jsp page


action class;


result jsp:


--

Could anyone please help me? I searcingfor thesolution all blogs and goodle, but still now no luck:)
Thanks
Thamayanthi
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello
i have problem white struts 2 validator

Tags <s:fielderror> you told me works well. Except that
Now I have duplicate messages (I thought the message "by
Default "no longer present).

please looks attchments

thank you in advance
valdator-struts.jpg
[Thumbnail for valdator-struts.jpg]
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi i have face the same problem and sorted out here is the solution:

Struts2 reads the file prepare tmp file and delete the tmp file after execute method
solution:

jsp code


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<s:head />
</head>
<body>
<s:form action="fileUpload" method="post" enctype="multipart/form-data" >
<s:file name="userImage" label="User Image" />
<s:submit />
</s:form>
</body>
</html>

Action Class

import java.io.File;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport{



/**
* Added serial version id
*/
private static final long serialVersionUID = -5138905699893319423L;

private File userImage;

private String userImageContentType;

private String userImageFileName;

private File upload;//The actual file
private String uploadContentType; //The content type of the file
private String uploadFileName; //The uploaded file name
private String fileCaption;//The caption of the file entered by user

public String execute() throws Exception {

try{

String fullFileName = "/opt/glassfish/domains/domain1/logs/myFile.txt;
File theFile = new File(fullFileName);

FileUtils.copyFile(userImage, theFile);

} catch (Exception e) {

addActionError(e.getMessage());

}


return SUCCESS;

}
public String getFileCaption() {
return fileCaption;
}
public void setFileCaption(String fileCaption) {
this.fileCaption = fileCaption;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}


public File getUserImage() {
return userImage;
}

public void setUserImage(File userImage) {
this.userImage = userImage;
}

public String getUserImageContentType() {
return userImageContentType;
}

public void setUserImageContentType(String userImageContentType) {
this.userImageContentType = userImageContentType;
}

public String getUserImageFileName() {
return userImageFileName;
}

public void setUserImageFileName(String userImageFileName) {
this.userImageFileName = userImageFileName;
}



}

struts xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">


<struts>
<package name="fileUploadPackage" extends="struts-default">
<interceptors>
<interceptor-stack name="fileUploadStack">
<interceptor-ref name="exception" />
<interceptor-ref name="alias" />
<interceptor-ref name="servletConfig" />
<interceptor-ref name="prepare" />
<interceptor-ref name="i18n" />
<interceptor-ref name="chain" />
<interceptor-ref name="debugging" />
<interceptor-ref name="profiling" />
<interceptor-ref name="scopedModelDriven" />
<interceptor-ref name="modelDriven" />
<interceptor-ref name="fileUpload">
<param name="maximumSize">10240</param>
<param name="allowedTypes">image/jpeg,image/gif,image/png</param>
</interceptor-ref>
<interceptor-ref name="checkbox" />
<interceptor-ref name="staticParams" />
<interceptor-ref name="actionMappingParams" />
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError" />
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>

<action name="fileUpload" class="vaannila.FileUploadAction">
<result name="input">/index.jsp</result>
<result name="success">/success.jsp</result>
</action>
</package>
</struts>

best of luck.

 
galne vaibhav
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben sassi mohammed wrote:hello
i have problem white struts 2 validator

Tags <s:fielderror> you told me works well. Except that
Now I have duplicate messages (I thought the message "by
Default "no longer present).

please looks attchments

thank you in advance




hi Ben sassi mohammed,


can you share your validation xml code and action code then it will be more clear i think you have done both xml action level validation also check for duplication of fielderror tag and actionerror tag.
 
reply
    Bookmark Topic Watch Topic
  • New Topic