/    Sign up×
Community /Pin to ProfileBookmark

uploading files and text

Hello…
I have a problem.. and i need some help with it..
this is my form in html:

[CODE]
<form method=”POST” name=”form2″ id=”form2″ enctype=’multipart/form-data’ action=”/servlet/uploadImage”>
<input type=”file”/>
</form>
[/CODE]

and this is the class im using:

[CODE]
public class uploadImage extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String fileName=processFile(request);
HttpSession sess = request.getSession();
if(fileName!=null){
sess.setAttribute(“nombreLogo”,fileName);
}else{
sess.setAttribute(“nombreLogo”,”null”);
}
}

public String processFile(HttpServletRequest req) {
String fileNameReturn=””;
try {
// object capable of parsing request
DiskFileUpload fu = new DiskFileUpload();

// max number of bytes
fu.setSizeMax(1024*100); // 100 K

fu.setSizeThreshold(4096);

//fu.setRepositoryPath(“/tmp”);

// file processing
List fileItems = fu.parseRequest(req);

if(fileItems == null)
{
System.out.println(“The list is null”);
return null;
}

System.out.println(“<br>The number of files uploaded is: ” + fileItems.size());

// We iterate on the files
Iterator i = fileItems.iterator();
FileItem actual = null;
System.out.println(“estamos en la iteración”);

while (i.hasNext())
{
actual = (FileItem)i.next();
String fileName = actual.getName();
System.out.println(“<br>File uploaded: ” + fileName);

// we create a file object
File fichero = new File(fileName);
System.out.println(“The file name is: ” + fichero.getName());
fileNameReturn=fichero.getName();
fichero = new File(“d:\path\” + fichero.getName());

actual.write(fichero);
}

}
catch(Exception e) {
System.out.println(e.getMessage());
return null;
}

return fileNameReturn;
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);

HttpSession sess = request.getSession();
if(String.valueOf(sess.getAttribute(“nombreLogo”)).equals(“null”)){
sess.setAttribute(“msg”,”El tamaño del archivo excede el máximo permitido.”);
}
response.sendRedirect(“/showroom/personalizaInteractivo.jsp”);

}

}
[/CODE]

What i want to do is to be able to put other types of “inputs” such as “text” on the html form, the problem im having is that when the class tries to process the request, it throws an exception because the first input is no “file”

i dont know if im being clear.. ask me for more info if you think it is necessary

I need to send something like this

[code=html]
<form method=”POST” name=”form2″ id=”form2″ enctype=’multipart/form-data’ action=”/servlet/uploadImage>
Name: <input type=”text” name=”name”/><br/>
Upload your picture: <input type=”file”/><br/>
<input type=”submit”/>
</form>
[/code]

and i need to insert those values in a database.

the class i got from a tutorial from here:
[url]http://www.adictosaltrabajo.com/tutoriales/tutoriales.php?pagina=fileupload[/url]
its in spanish though..

thanks in advance

to post a comment
Java

2 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliJan 22.2007 — you need to differentiate that if a field is a form field or its a file item...
[code=php]
FileItem item = (FileItem) iter.next();

if (item.isFormField()) {
//process form field data here
} else {
//process file data here
}
[/code]


Hope this helps
Copy linkTweet thisAlerts:
@Leon945authorJan 26.2007 — ?

thanks!!

I didnt know that property existed.. that DID work for me..

thanks again
×

Success!

Help @Leon945 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...