Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to show image files after uploading these images..i have a html file and Servlet file where i'm uploading some images,but i want to show those images through a html page where user can view them..can it be possible??plz guide me what should i do??i'm sending html page and servlet page,plz help...

HTML file

<html>
<body>
<form action="image" method="post" enctype="multipart/form-data">
Select File:<input type="file" name="fname"/>

<input type="submit" value="upload"/>
</form>
</body>
</html>

and Servlet File

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
* A Java servlet that handles file upload from client.
*
* @author www.codejava.net
*/
public class image extends HttpServlet {
private static final long serialVersionUID = 1L;

// location to store file uploaded
private static final String UPLOAD_DIR = "upload";

// upload settings
private static final int MEMORY_THRESHOLD = 1024 * 1024 * 3; // 3MB
private static final int MAX_FILE_SIZE = 1024 * 1024 * 40; // 40MB
private static final int MAX_REQUEST_SIZE = 1024 * 1024 * 50; // 50MB

/**
* Upon receiving file upload submission, parses the request to read
* upload data and saves the file on disk.
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// checks if the request actually contains upload file
if (!ServletFileUpload.isMultipartContent(request)) {
// if not, we stop here
PrintWriter writer = response.getWriter();
writer.println("Error: Form must has enctype=multipart/form-data.");
writer.flush();
return;
}

// configures upload settings
DiskFileItemFactory factory = new DiskFileItemFactory();
// sets memory threshold - beyond which files are stored in disk
factory.setSizeThreshold(MEMORY_THRESHOLD);
// sets temporary location to store files
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));

ServletFileUpload upload = new ServletFileUpload(factory);

// sets maximum size of upload file
upload.setFileSizeMax(MAX_FILE_SIZE);

// sets maximum size of request (include file + form data)
upload.setSizeMax(MAX_REQUEST_SIZE);

// constructs the directory path to store upload file
// this path is relative to application's directory
String uploadPath = getServletContext().getRealPath("")
+ File.separator + UPLOAD_DIR;

// creates the directory if it does not exist
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdir();
}

try {
// parses the request's content to extract file data
@SuppressWarnings("unchecked")
List<fileitem> formItems = upload.parseRequest(request);

if (formItems != null && formItems.size() > 0) {
// iterates over form's fields
for (FileItem item : formItems) {
// processes only fields that are not form fields
if (!item.isFormField()) {
String fileName = new File(item.getName()).getName();
String filePath = uploadPath + File.separator + fileName;
File storeFile = new File(filePath);


item.write(storeFile);
request.setAttribute("message",
"Upload has been done successfully!");
}
}
}
} catch (Exception ex) {
request.setAttribute("message",
"There was an error: " + ex.getMessage());
}

getServletContext().getRequestDispatcher("/message.jsp").forward(request, response);
}
}

Truly speaking i got this servlet code from internet..if this code wrong plz help...plz plz ...
Posted

1 solution

Consider the tasks as separate.

Task one:
upload the file with your servlet, or by any means you wish. You should control the file name and location for later use.

Task Two: load an image file from your server after the page has already rendered.

Use AJAX to upload the file and you can stay on your current page.
Use a form and you'll need to load a new page, even if it's the same page.

In either case, you have the image file's name and location.
AJAX: upload the image file by to the location where you want it to go.
forms: when you invoke you're new page, build the image files' name into the header and use javaScript to load the image.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900