Currently i am uploading one image from browser.Now in back end there is java code there i am retrieving this images and converting into byte array and storing into database this part working actually fine
The Code goes like this:
String fromLocal = "D:/123.jpg";
File file = new File(fromLocal);
InputStream inputStream = null;
byte[] bFile= null;
byte[] imageData = null;
try {
inputStream = new BufferedInputStream(new FileInputStream(file));
ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);
bFile = new byte[8192];
int count;
while((count = inputStream.read(bFile))> 0){
baos.write(bFile, 0, count);
}
bFile = baos.toByteArray();
if(bFile.length > 0){
imageData = bFile;
}
baos.flush();
inputStream.close();
} catch (Exception ioe) {
//throw ioe;
}
Problem is whenever i am trying to hardcode the image path (like here D:/123.jpg) it is working absolutely fine. Now its user dependent & client dependent he can able to load the image from any drive & from any directory.I dont have privilege of using servlet. My query is :
1.Now,if i am trying to upload the same image from D:/123.jpg from browser i am getting only 123.jpg not the absolute path like D:/123.jpg. Because of the same now not able to process the image.
2.How to know that particular path from where user was trying to upload the image.(lets say user uploading image from C:/images/123.jpg),so how to get this absolute path.
I tried my best to detailed my problem,let me know if something is not clear i will try to explain in some other way.
If the user is uploading a file to your servlet, the file is contained in the request body, it's not on any path on the server. The path on the end user's client machine is irrelevant (and you can't access it anyway, not even client-side).
There's a Java EE 6 tutoral on file upload here: http://docs.oracle.com/javaee/6/tutorial/doc/glraq.html
Fundamentally (from that example), if the field used for upload has the name file
, you do this in your servlet:
final Part filePart = request.getPart("file");
InputStream filecontent = null;
and then later within a try/catch
:
filecontent = filePart.getInputStream();
...and use the data from the stream.
Here's the source from the tutorial above (in case it's inaccessible when someone's reading this later). In this case, it's writing the file out to a file server-side, but in your case of course you would populate your imageData
(which I take it you then put in a DB) instead.
@WebServlet(name = "FileUploadServlet", urlPatterns = {"/upload"})
@MultipartConfig
public class FileUploadServlet extends HttpServlet {
private final static Logger LOGGER =
Logger.getLogger(FileUploadServlet.class.getCanonicalName());
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
// Create path components to save the file
final String path = request.getParameter("destination");
final Part filePart = request.getPart("file");
final String fileName = getFileName(filePart);
OutputStream out = null;
InputStream filecontent = null;
final PrintWriter writer = response.getWriter();
try {
out = new FileOutputStream(new File(path + File.separator
+ fileName));
filecontent = filePart.getInputStream();
int read = 0;
final byte[] bytes = new byte[1024];
while ((read = filecontent.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
writer.println("New file " + fileName + " created at " + path);
LOGGER.log(Level.INFO, "File{0}being uploaded to {1}",
new Object[]{fileName, path});
} catch (FileNotFoundException fne) {
writer.println("You either did not specify a file to upload or are "
+ "trying to upload a file to a protected or nonexistent "
+ "location.");
writer.println("<br/> ERROR: " + fne.getMessage());
LOGGER.log(Level.SEVERE, "Problems during file upload. Error: {0}",
new Object[]{fne.getMessage()});
} finally {
if (out != null) {
out.close();
}
if (filecontent != null) {
filecontent.close();
}
if (writer != null) {
writer.close();
}
}
}
private String getFileName(final Part part) {
final String partHeader = part.getHeader("content-disposition");
LOGGER.log(Level.INFO, "Part Header = {0}", partHeader);
for (String content : part.getHeader("content-disposition").split(";")) {
if (content.trim().startsWith("filename")) {
return content.substring(
content.indexOf('=') + 1).trim().replace("\"", "");
}
}
return null;
}
}