This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Multipart Upload - where does my file go?

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have a servlet that uses Multipart requests to upload files.
I am running tomcat3.2 as my webserver. I want to upload a file from my C: drive to the Web Server. I create a multipart request in the servlet like so -
MultipartRequest multi = new MultipartRequest(req, "e:\\temp", 5 * 1024 * 1024);
However, when I run the servlet, it does not put the file into the folder I specified......what am I doing wrong?
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what *does* it do?

If you surround the code with try-catch, what does the thrown exception report? Does it not throw an exception? Does it put the file somewhere else?
 
Dave Donohoe
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there Mike,
Let me explain a bit better. I have a servlet called UploadTest & This is called from a HTML Form.
At the top of this HTML Form I have the following -
<form method="post" action="..\UploadTest" name="Box" ENCTYPE="multipart/form-data">
<input type="file" name="hello">
This places a Text Box & a browse button on the form (which I did not notice yesterday as it was behind other components).
When I hit browse & select a file it uploads the file as expected. But, I dont want to give the user the option to select the file as I want to hardcode the name of the file in the code.
How do I do this?
This is the servlet code -
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.oreilly.servlet.MultipartRequest;
public class UploadTest extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();

try {
// Blindly take it on faith this is a multipart/form-data request
// Construct a MultipartRequest to help read the information.
// Pass in the request, a directory to save files to, and the
// maximum POST size we should attempt to handle.
// Here we (rudely) write to the server root and impose 5 Meg limit.
MultipartRequest multi = new MultipartRequest(req, "e:\\temp", 5 * 1024 * 1024);

out.println("<HTML>");
out.println("<HEAD><TITLE>UploadTest</TITLE></HEAD>");
out.println("<BODY>");
out.println("<H1>UploadTest</H1>");
// Print the parameters we received
out.println("<H3>Params:</H3>");
out.println("<PRE>");
Enumeration params = multi.getParameterNames();

while (params.hasMoreElements()) {
String name = (String)params.nextElement();
System.out.println("~~" + name);
String value = multi.getParameter(name);
out.println(name + " = " + value);
}
out.println("</PRE>");
// Show which files we received
out.println("<H3>Files:</H3>");
out.println("<PRE>");
Enumeration files = multi.getFileNames();
while (files.hasMoreElements()) {
String name = (String)files.nextElement();
System.out.println("NAME - " + name);
String filename = multi.getFilesystemName(name);
System.out.println("FILENAME - " + filename);
String type = multi.getContentType(name);
System.out.println("TYPE - " + type);
File f = multi.getFile(name);
out.println("name: " + name);
out.println("filename: " + filename);
out.println("type: " + type);
if (f != null) {
out.println("length: " + f.length());
}
out.println();
}
out.println("</PRE>");
}
catch (Exception e) {
out.println("<PRE>");
e.printStackTrace(out);
out.println("</PRE>");
}
out.println("</BODY></HTML>");
}
}

Basically, I want to be able to upload files without any direct interaction from the user.
Thanks,
D
 
That feels good. Thanks. Here's a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!