A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
The Mikado Method
this week in the
Agile and other Processes
forum!
JavaRanch
»
Java Forums
»
Frameworks
»
Struts
Author
How to store image file in specific folder
Satyajeet Kadam
Ranch Hand
Joined: Oct 19, 2006
Posts: 202
posted
Feb 02, 2007 04:05:00
0
I am doing application in
struts
, in that iam uploading image file
Q1)Now i want to store that file under root context in image folder?
Q2) Is it possiable to store image in mysql? if possiable ? how?
I am pastin my code
package roseindia.net; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.awt.image.BufferedImage; import java.io.*; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.upload.FormFile; public class StrutsUploadAction extends Action { HttpSession session=null; String name=null; File outfile=null; public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { StrutsUploadForm myForm = (StrutsUploadForm)form; try { [B] BufferedImage bi =(BufferedImage) myForm.getTheFile();// retrive image // getting root context // String path=request.getContextPath()+"/src"; System.out.println("path changetd==="+path); //Saving image in file// outfile = new File("path"); // ImageIO.write();[/B] } catch(Exception e) { e.printStackTrace(); } // Process the FormFile FormFile myFile = myForm.getTheFile(); String contentType = myFile.getContentType(); String fileName = myFile.getFileName(); String name=(String)session.getAttribute("satya"); System.out.println("getAttribute=="+name); String newfile=fileName.replaceAll("fileName","name"); int fileSize = myFile.getFileSize(); byte[] fileData = myFile.getFileData(); System.out.println("contentType: " + contentType); System.out.println("File Name: " + fileName); System.out.println("File Size: " + fileSize); return mapping.findForward("success"); } }
Satyajeet Kadam
Ranch Hand
Joined: Oct 19, 2006
Posts: 202
posted
Feb 02, 2007 04:37:00
0
Retriving the image from form bean and displaying it height gives error
[B] BufferedImage bi =(BufferedImage) myForm.getTheFile();// retrive image System.out.println("height of image"+bi.getHeight());[/B]
java.lang.ClassCastException: org.apache.struts.upload.CommonsMultipartRequestHa ndler$CommonsFormFile at roseindia.net.StrutsUploadAction.execute(StrutsUploadAction.java:37) at org.apache.struts.action.RequestProcessor.processActionPerform(Reques tProcessor.java:431) at org.apache.struts.action.RequestProcessor.process(RequestProcessor.ja va:236) at org.apache.struts.action.ActionServlet.process(ActionServlet.java:119 6) at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432) at javax.servlet.http.HttpServlet.service(HttpServlet.java:709) at javax.servlet.http.HttpServlet.service(HttpServlet.java:802) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl icationFilterChain.java:252) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF ilterChain.java:173) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV alve.java:213) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV alve.java:178) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
Joan Horta Tosas
Ranch Hand
Joined: Feb 01, 2007
Posts: 59
posted
Feb 02, 2007 05:13:00
0
That's because your trying to convert a FormFile object into a
BufferedImage
in this line
try { BufferedImage bi =[B](BufferedImage) myForm.getTheFile();[/B]//retrive image ... } catch(Exception e) { e.printStackTrace(); }
I suppose that myForm.getTheFile() returns a FormFile because you later have this line:
// Process the FormFile FormFile myFile = myForm.getTheFile();
Merrill Higginson
Ranch Hand
Joined: Feb 15, 2005
Posts: 4864
posted
Feb 02, 2007 11:10:00
0
Q2) Is it possiable to store image in mysql? if possiable ? how?
Yes. You can store it in a BLOB.
This link
shows you how.
Merrill
Consultant,
Sima Solutions
I agree. Here's the link:
http://zeroturnaround.com/jrebel
- it saves me about five hours per week
subject: How to store image file in specific folder
Similar Threads
HOw to store image in struts?upload.FormFile
Problem with file separator
Map the action form object
File type and size
Why my action class in not executing?
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter