Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Uploading

 
amit bhadre
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to upload a file want to store in one folder/database. So How to do that?

It should like attachment in mails.


if u give answer thnks with code....





cheers
amit bhadre
 
Satish SN
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amit,

import org.apache.struts.action.ActionForm;
9 import org.apache.struts.action.ActionMapping;
10 import org.apache.struts.action.ActionErrors;
11 import org.apache.struts.action.ActionMessage;
12 import org.apache.struts.upload.FormFile;
13 import javax.servlet.http.HttpServletRequest;
14
15 /***
16 *
17 * @author Sean C. Sullivan
18 *
19 */
20 public class FileUploadForm extends ActionForm
21 {
22 private FormFile file;
23
24 public void setFile(FormFile f)
25 {
26 this.file = f;
27 }
28
29 public FormFile getFile()
30 {
31 return this.file;
32 }
33
34 public void reset(ActionMapping mapping, HttpServletRequest request)
35 {
36 this.file = null;
37 }
38
39 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
40 {
41 ActionErrors errors = new ActionErrors();
42
43 if (this.getFile() == null)
44 {
45 errors.add("file", new ActionMessage("error.file.missing"));
46 }
47 else if (this.getFile().getFileName() == null)
48 {
49 errors.add("file", new ActionMessage("error.empty.filename"));
50 }
51 else if (this.getFile().getFileName().length() < 1)
52 {
53 errors.add("file", new ActionMessage("error.empty.filename"));
54 }
55 else if (this.getFile().getFileSize() < 1)
56 {
57 errors.add("file", new ActionMessage("error.file.size"));
58 }
59 return errors;
60 }
61
62 public String toString()
63 {
64 return "file=" + String.valueOf(this.getFile());
65 }
66 }



import org.apache.struts.action.Action;
8 import org.apache.struts.action.ActionMapping;
9 import org.apache.struts.action.ActionForm;
10 import org.apache.struts.action.ActionForward;
11 import org.apache.struts.upload.FormFile;
12
13 import example.filestorage.*;
14
15 import javax.servlet.http.HttpServletRequest;
16 import javax.servlet.http.HttpServletResponse;
17 import java.io.InputStream;
18
19 /***
20 *
21 * @author Sean C. Sullivan
22 *
23 *
24 *
25 */
26 public class FileUploadAction extends Action
27 {
28 public ActionForward execute(ActionMapping mapping,
29 ActionForm form,
30 HttpServletRequest request,
31 HttpServletResponse response) throws Exception
32 {
33 FileUploadForm uploadForm = (FileUploadForm) form;
34
35 FormFile file = uploadForm.getFile();
36
37 ActionForward forward = null;
38
39 InputStream input = null;
40
41 try
42 {
43 int fileSize = file.getFileSize();
44 String fileName = file.getFileName();
45
46 input = file.getInputStream();
47
48 FileStorageDAO dao = DAOFactory.getFileStorageDAO();
49
50 dao.saveFile(fileName, input, fileSize);
51
52 forward = mapping.findForward(Forward.KEY_SUCCESS);
53 }
54 finally
55 {
56 if (input != null)
57 {
58 input.close();
59 }
60 file.destroy();
61 }
62
63 return forward;
64 }
65
 
Satish SN
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amit

following link explains the file upload functionality in struts

http://javaboutique.internet.com/tutorials/struthiber/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic