0

How can i move file to hosting? I use this code but it doesn't work.

File uploads = new File("/Users/Aurimo/Desktop/");
File file = new File(request.getParameter("dataFile"));
    file.renameTo(uploads);
Aurimas_12
  • 109
  • 1
  • 2
  • 13
  • possible duplicate of [How to upload files to server using JSP/Servlet?](http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet) - A simple *jsp upload* search inside SO immediately leads there ... – Serge Ballesta Feb 16 '15 at 06:48

3 Answers3

0

For creating jsp page for upload purpose, You create database, folder in file system, display upload form, then display result. These below links will be helpful for you:

http://www.codejava.net/coding/upload-files-to-database-servlet-jsp-mysql http://www.tutorialspoint.com/jsp/jsp_file_uploading.htm

Vy Do
  • 46,709
  • 59
  • 215
  • 313
0
use multipart form 

and server side use following code.

for(FileItem item : multiparts){
    if(!item.isFormField()){
        String name = new File(item.getName()).getName();
        item.write( new File(UPLOAD_DIRECTORY + File.separator + name));
        request.setAttribute("photoname", name);
    }else{
        // here get value of other parameter which is not file type
        System.out.println(item.getFieldName()+" = "+item.getString());
    }
}
Sanjay
  • 2,481
  • 1
  • 13
  • 28
0
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(request);
for (FileItem item : items) {
    if (item.isFormField()) {  //als het een veld is dan dit, anders File uploaden
        String fieldname = item.getFieldName();
        String fieldvalue = item.getString();
        switch (fieldname) {
            case "logicalName":
                LogicalName = fieldvalue;
                break;
            //other case statements...
        }
    } else {
        //here you only have to process the file
        File file;
        int maxFileSize = 500000 * 1024;//your comments...
        int maxMemSize = 5000 * 1024;//your comments...
        //this must be a constant or a servlet init param, do not hard code it
        String filePath = "C:\\uploads\\";
        String fileName = FilenameUtils.getName(item.getName());
        factory.setSizeThreshold(maxMemSize);
        //didn't you have a filePath variable?
        factory.setRepository(new File("c:\\temp"));
        upload.setSizeMax(maxFileSize);
        try {
            String fieldName = fi.getFieldName();
            boolean isInMemory = fi.isInMemory();
            long sizeInBytes = fi.getSize();
            file = new File(filePath, fileName);
            item.write(file);
            //code to save your file location in db...
            //note: this MUST BE in a business logic method, not directly written in your servlet
            HttpSession session = request.getSession();
            int uploader = (Integer) session.getAttribute("UserId");
        } catch (Exception ex) {
            //very BAD idea
            //use a logger instead like log4j or sfl4j
            System.out.println(ex);
        }
    }
}
Sanjay
  • 2,481
  • 1
  • 13
  • 28