To upload a file we will use the libraries of struts. First What we are going to create a form that will take care of accessor methods for the file that we will save that type of FormFile struts.
package com.myapp.struts.action;
import org.apache.struts.action . ActionForm;
import org.apache.struts.upload.FormFile;
/
** * * @ author
r.sheshi
* / public class UploadFile
extends ActionForm {private
FormFile file;
public FormFile getFile () {
return file;}
public void SetFile (FormFile file) {
this.file = file;}
}
After defining the form to create JSPs that will do the upload.
\u0026lt;html:form action="/upload" enctype="multipart/form-data">
\u0026lt;html:file property="file" />
\u0026lt;html:submit value="carica" \u200b\u200b
/> \u0026lt;/ html: form>
The form must have the enctype set to "multipart / form-data" and use the tag html: file with propetrty set to "file" as mapped in our form. Now let's create our action.
/
** * * @ author
r.sheshi
* / public class
CaricaFileAction extends Action {
@ Override public ActionForward execute (ActionMapping mapping
,
ActionForm form, HttpServletRequest request
,
HttpServletResponse response) throws Exception {
DynaActionForm f = (DynaActionForm) form;
FormFile ff = (FormFile) f.get ("file");
return mapping.findForward ("success");}
}
The action we take the file from the bean that has struts valorizzato.Dopo taking the file you should go and inserilo blob in our field. Thus we create an object that we will take care of transferring data from the action of the Dao that ocuperà place.
/
** * * @ author
r.sheshi
* / public class
FileDto
{private int id;
private byte [] bytes;
private int size;
private String name;
private String contentType;
public void setBytes(byte[] bytes) {
this.bytes = bytes;
}
public byte[] getBytes() {
return bytes;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public void setSize(int size) {
this.size = size;
}
public void setContentType(String type) {
this.type = type;
} public String getName () {
return name;}
public int getSize () {return size
;
} public String getContentType () {return type
;
}}
Now that we have our Dto that has a class variable array of bytes that will enhance it in action by using the method FormFile getFileData ().
DynaActionForm f = (DynaActionForm) form;
FormFile ff = (FormFile) f.get ("file");
FileDto FileDto dto = new ();
dto.setBytes (ff.getFileData ());
dto.setContentType (ff.getContentType ());
dto.setSize (ff.getFileSize ());
dto.setName (ff.getFileName ());
Now that we have enhanced our Dto passes it to the DAO to insert it into the database.
FileDao FileDao dao = new ();
dao.insert (dto);
Then in FileDao esguiamo operations for inseriento FILE in our table, which will have 4 fields (ID, FILE ( BLOB), name (VARCHAR), CONTENT_TYPE (VARCHAR)).
private String INSERT = "INSERT INTO VALUES FILE (?,?,?,?)";
public void insert (FileDto dto) {PreparedStatement ps
= Null;
Connection con = null;
try {con = getConnection
();
con.prepareStatement ps = (INSERT);
ps.setInt (1, dto.getId)
ps.setBytes (2 , dto.getBytes ());
ps.setString (3, dto.getName ());
ps.setString (4, dto.getContentType ());
ps.executeUpdate ();
} catch (SQLException ex)
{Logger.getLogger (FileDao.class.getName ())
. log (Level.SEVERE, null, ex);} finally {
try {if (ps! = null) ps.close ();
if (with! = null) con.close ();
} catch (SQLException ex) {Logger.getLogger
(FileDao.class.getName ())
. log (Level.SEVERE, null, ex);}
}}
At this point we will have our table inside the file and all information.
How do we get the file when we need it?
Now that we have our files in the database we create an action to display a list of files.
add a method to the Dao, which will make a select returning an ArrayList of Dto enhanced with only id and name.
listaFile public ArrayList () {
PreparedStatement ps = null;
ResultSet rs = null;
Connection con = null;
DTOS ArrayList = new ArrayList ();
try {con = getConnection () ;
con.prepareStatement ps = (SELECT);
ps.executeQuery rs = ();
while (rs.next ()) {dto = new
FileDto FileDto ();
dto.setId (rs.getInt (1) )
dto.setName (rs.getString (2));
dtos.add (dto);
}} catch (SQLException ex) {Logger.getLogger
(FileDao.class.getName ())
. Log (Level.SEVERE, null, ex);} finally {
try {if (rs! = Null) ps.close ();
if (ps! = null) ps.close ();
if (with! = null) con.close ();
} catch (SQLException ex) {Logger.getLogger
(FileDao.class . getName ())
. log (Level.SEVERE, null, ex);}
}
DTOS return;}
Then we create a new Action that calls the listaFile method that will have as a return mettermo in our collection that request and then sliding it in jsp.
ListaFileAction extends Action {public class
@ Override public ActionForward execute (ActionMapping mapping, ActionForm form
,
HttpServletRequest request, HttpServletResponse
response) throws Exception {
FileDao FileDao dao = new ();
ArrayList DTOS dao.listaFile = ();
request.setAttribute ("list", DTOS)
return mapping.findForward ("list");
}}
Now that we have our list in jsp visualizziamola as a series of links to click that will call a action passing id file to download.
\u0026lt;html:html>
\u0026lt;logic:present name="lista">
\u0026lt;logic:iterate name="lista" id="dto">
\u0026lt;html: link action = "/ download" aramid = "fileid"
paramName = "dto" paramProperty = "id">
\u0026lt;bean:write name="dto" property="name"
/> \u0026lt;/ html: link>
\u0026lt;/ logic: iterate>
\u0026lt;/ logic: present>
\u0026lt;/ html: html>
proceed to create the action that handles the download of files by the ID that will pass in link will retrieve the file in the database by calling the Dao.
SELECT_BY_ID private String = "Select * from files where id =?"
public FileDto findById (int id) {
PreparedStatement ps = null;
ResultSet rs = null;
Connection con = null;
FileDto dto = FileDto new ();
try {con = getConnection
();
con.prepareStatement ps = (SELECT_BY_ID);
ps.setInt (1, id);
ps.executeQuery rs = ();
while (rs.next ()) {Blob blob =
rs.getBlob (2);
byte [] bytes = blob.getBytes (1, (int) blob.length ());
dto.setId (rs.getInt (1));
dto.setBytes (bytes);
dto.setName (Rs. getString (3));
dto.setContentType (rs.getString (4));
}} catch (SQLException ex) {Logger.getLogger
(FileDao.class.getName ())
. Log (Level.SEVERE, null, ex);} finally {
try {if (rs! = Null) ps.close ();
if (ps! = Null) ps.close (); if
(with! = null) con.close ();
} catch (SQLException ex) {Logger.getLogger
(FileDao.class.getName ())
. log (Level.SEVERE, null, ex);}
}
return dto;}
After calling the method findById () of the Dao we set the ContentType in the response depending on the type of file. In the attachment so that we set Header be downloaded on the local and the name of the file. Then get ServletOutputStream and write the byte array.
DownloadAction extends Action {public class
@ Override public ActionForward execute (ActionMapping mapping, ActionForm form
,
HttpServletRequest request, HttpServletResponse
response) throws Exception {
FileDao FileDao dao = new ();
String id = request.getParameter ("fileid");
FileDto dao.findById dto = (Integer.parseInt (id));
response.setContentType (dto.getContentType ());
response.setHeader ("Content-Disposition",
"attachment; filename = \\" "+ dto.getName () +" \\ "");
final ServletOutputStream response.getOutputStream out = ();
out.write ( dto.getBytes ());
return null;}
}
0 comments:
Post a Comment