Due to the 300 line limit for postings, I had to separate my posts.
Combine the following code with Part 3: file upload Java/HTML code.
*---------------------*
//---------------------------------------------------------------------------
// File: FileUploadServlet.java
// Author: Java Web Server
//---------------------------------------------------------------------------
// Programmer: Dewey C. Layman
// Modified Date: April 25, 1999
// Description: Made various personal cosmetic changes as well
// as using the destination directory upload.
//---------------------------------------------------------------------------
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FileUploadServlet extends HttpServlet {
// By default, 6Mb max file size per file;
protected int maxSize = 1024 * 1024 * 6; // 6MB
public void init(ServletConfig sc)
{
String max = sc.getInitParameter("maxSize");
try {
maxSize = Integer.parseInt(max);
}
catch (NumberFormatException e) {
// Do nothing, leave at default value
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{
Hashtable table;
if
(!request.getContentType().toLowerCase().startsWith("multipart/form-data")) {
// Since this servlet only handles File Upload, bail out
// Note: this isn't strictly legal - I should send
// a custom error message
sendFailure(response, "Wrong content type set for file upload");
return;
}
if (request.getContentLength() > maxSize) {
sendFailure(response, "Content too long - sent files are limited in
size");
return;
}
int ind = request.getContentType().indexOf("boundary=");
if (ind == -1) {
sendFailure(response, "boundary is not set");
return;
}
String boundary = request.getContentType().substring(ind + 9);
if (boundary == null) {
sendFailure(response, "boundary is not set");
return;
}
try {
table = parseMulti(boundary, request.getInputStream());
}
catch (Throwable t) {
t.printStackTrace(new PrintStream(response.getOutputStream()));
return;
}
ServletOutputStream out = response.getOutputStream();
out.println("<html>");
out.println("<head>");
out.println("<title>");
out.println(" Java Servlet Upload Utility");
out.println("</title>");
out.println("</head>");
out.println("<body>");
for (Enumeration fields = table.keys(); fields.hasMoreElements(); ) {
Object obj = table.get((String)fields.nextElement());
// directory is set to the upload directory in this directory
// this can be modified to the reference the desired upload directory
String path = request.getPathTranslated() + "upload", file = "";
int dirDelimiterPos = 0;
if (obj instanceof Hashtable) {
// verified true file
Hashtable filehash = (Hashtable)obj;
// Get the exact filename from the filename variable
for (int i = dirDelimiterPos; i <
filehash.get("filename").toString().length(); i++)
if (filehash.get("filename").toString().charAt(i) == '\\' ||
filehash.get("filename").toString().charAt(i) == '/')
dirDelimiterPos = i;
for (int i = dirDelimiterPos; i <
filehash.get("filename").toString().length(); i++)
file += filehash.get("filename").toString().charAt(i);
// write uploaded file contents to file on server
FileOutputStream fos = new FileOutputStream(path + file);
obj = filehash.get("content");
byte[] bytes = (byte[])obj;
fos.write(bytes, 0, bytes.length);
fos.close();
// display upload information to client
out.println("<b>File received:</b> " + filehash.get("filename") +
"<br>");
out.println("<b>File saved as:</b> " + path + file + "</b><br>");
out.println("<b>Content-Type:</b> " + filehash.get("content-type") +
"<br>");
}
else if (obj instanceof String[]) {
String[] values = (String[])obj;
for (int i = 0; i < values.length; i++)
out.println(values[i] + "<br>");
}
out.println("</body>");
out.println("</html>");
}
out.flush();
out.close();
}
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html