Uploader un fichier vers un repertoire précis
Bonjour tout Le monde ,
J'ai besoin de mettre à la disposition d'un client qui utilise mon application web(struts,hibernate comme freamwork ) ,d'uploder un fichier .
Ce ficher dois appraitre après sous forme d'un lien de telechargment dans une autre page.Pour gérer ces fcihiers je dois les mettre dans un repertoire par exemple un repertoire dans mon application nommé ..../webapp/monAplli/upload .
voila le code de mon action :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
public class ProduitajtAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)throws Exception {
ProduitajtForm aj = (ProduitajtForm) form;
FormFile myFile = aj.getFichier();
//String contentType = myFile.getContentType();
//Get the file name
String fileName = myFile.getFileName();
// byte[] fileData = myFile.getFileData();
String filePath = getServlet().getServletContext().getRealPath("/") +"upload";
/* Save file on the server */
if(!fileName.equals("")){
System.out.println("Serveur path:" +filePath);
//Create file
File fileToCreate = new File(filePath, fileName);
if(!fileToCreate.exists()){
FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);
fileOutStream.write(myFile.getFileData());
fileOutStream.flush();
fileOutStream.close();
} } |
Je me suis inspiré d'un code trouvé sur internet mais j'arrive pas encore au bout de mon objectif.