Example usage for org.apache.commons.fileupload FileItem getString

List of usage examples for org.apache.commons.fileupload FileItem getString

Introduction

In this page you can find the example usage for org.apache.commons.fileupload FileItem getString.

Prototype

String getString(String encoding) throws UnsupportedEncodingException;

Source Link

Document

Returns the contents of the file item as a String, using the specified encoding.

Usage

From source file:com.shyslav.controller.UploadController.java

@RequestMapping(value = "/uploadAction")
private String uploadAction(ModelMap mv, RedirectAttributes redirectAttributes, HttpSession ses,
        HttpServletRequest request) throws URISyntaxException {
    String name = null;//from  ww  w . j  a  v a2 s. c  om
    String genre = null;
    String author = null;
    String smallText = null;
    String fullText = null;
    String vision = null;
    String serverPath = null;
    Properties props = new Properties();
    try (InputStream in = UploadController.class.getResourceAsStream("application.properties")) {
        props.load(in);
    } catch (IOException ex) {
        System.out.println(ex);
    }
    DiskFileItemFactory d = new DiskFileItemFactory();
    ServletFileUpload uploadre = new ServletFileUpload(d);
    System.out.println(props.getProperty("downloadDirectory"));
    try {
        List<FileItem> list = uploadre.parseRequest(request);
        for (FileItem f : list) {
            if (f.isFormField() == false) {
                //write file to upload folder;

                if (!FilenameUtils.getExtension(f.getName()).equals("pdf")) {
                    String ext = FilenameUtils.getExtension(f.getName());
                    System.out.println(ext);
                    System.out.println("comed");
                    redirectAttributes.addFlashAttribute("error",
                            "?   ? , ?  pdf");
                    return "redirect:add.htm";
                }
                serverPath = "/" + author + "_" + name + "_" + genre + "_" + Math.random() * 100 + "."
                        + FilenameUtils.getExtension(f.getName());
                f.write(new File(props.getProperty("downloadDirectory") + serverPath));
                System.out.println(f.getName());
            } else if (f.isFormField()) {
                String fieldname = f.getFieldName();
                if (fieldname.equals("name")) {
                    name = f.getString("UTF-8");
                } else if (fieldname.equals("genre")) {
                    genre = f.getString("UTF-8");
                } else if (fieldname.equals("author")) {
                    author = f.getString("UTF-8");
                } else if (fieldname.equals("smallText")) {
                    smallText = f.getString("UTF-8");
                } else if (fieldname.equals("fullText")) {
                    fullText = f.getString("UTF-8");
                } else if (fieldname.equals("vision")) {
                    vision = f.getString("UTF-8");
                }
                System.out.println(f.getFieldName().toString());
            }
        }
    } catch (Exception ex) {
        System.out.println(ex);
    }
    if (name == null || genre == null || author == null || smallText == null || fullText == null
            || vision == null || smallText.length() < 30 || fullText.length() < 50 || name.length() < 3) {
        redirectAttributes.addFlashAttribute("error",
                "? ? ?  ");
        return "redirect:add.htm";
    }
    try {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Query query = session.createSQLQuery("INSERT INTO books\n"
                + "(name, author, genre, small_text, full_text, date_create, vision, server_Path)\n"
                + "VALUES('" + name + "', '" + author + "', " + genre + ", '" + smallText + "', '" + fullText
                + "', NOW(), '" + vision + "','" + serverPath + "')");
        System.out.println(query.toString());
        int result = query.executeUpdate();
        session.getTransaction().commit();
    } catch (Exception ex) {
        System.out.println(ex);
    }
    redirectAttributes.addFlashAttribute("sucses", " ? ");
    return "redirect:index.htm";
}