• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

How to Upload Files

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody give me a link to a tutorial that describes file uploading in a simple way?
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you looked through the Servlet and JSP FAQs?
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Servlet 3.0 @MultiPartConfig

JSP page:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Upload File</title>
</head>
<body>
<form action="uploadTest" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>Select File : </td>
<td><input name="fileToUpload" type="file"/> </td>
</tr>
</table>
<p/>
<input type="submit" value="Upload File"/>
</form>
</body>
</html>

Actual Servlet:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns="/uploadTest")
@MultipartConfig
public class MySecondServlet extends HttpServlet {

private static final long serialVersionUID = 11234354643L;

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response){
try {
String fileName = MySecondServlet.getFileName(request.getPart("fileToUpload").getHeader("content-disposition"));
String outPutFile = this.getServletContext().getRealPath(fileName);
FileOutputStream os = new FileOutputStream(outPutFile);

InputStream is = request.getPart("fileToUpload").getInputStream();
int ch = is.read();
while(ch != -1){
os.write(ch);
ch = is.read();
}
os.close();
response.getWriter().append("Uploaded!");

} catch (IOException e) {
e.printStackTrace();
} catch (ServletException e) {
e.printStackTrace();
}
}

private static String getFileName(String neededHeader){
String fileName = null;
for(String onePiece : neededHeader.split(";") ){
if(onePiece.contains("filename=")){
String myPieces[] = onePiece.split("=");
fileName = myPieces[1].replaceAll("\"", "").trim();
}
}
// If we reach this if, then either the Header is deformated or it does not have the needed format
if(null == fileName) throw new IllegalArgumentException("The Header provided seems to be Invalid!");
return fileName;
}
}
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eugene Rabii, Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard. Also, be sure that someone is using a Servlet 3.0 container before posting a Servlets 3.0-specific solution. Such containers are not yet in wide use. Thanks.
 
Eugene Rabii
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes sir!
 
He was expelled for perverse baking experiments. This tiny ad is a model student:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic