title: File upload change filename in java
body: <p>File upload change filename in java serclet.How to rename a file (change file name) in java servlet.Rename and save the database.please help me.</p>
  
  <p>my file upload code. </p>
  
  <pre><code>public class AjaxImage extends MNServlet {
      private static URLCodec urlcodec = new URLCodec("UTF-8");
      private static final long serialVersionUID = 1L;
  
      protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
          boolean isMultipart = ServletFileUpload.isMultipartContent(request);
           String id = request.getParameter(Plugin.PLUGIN_ID_VARIABLE);
  
          if (isMultipart) {          
              FileItemFactory factory = new DiskFileItemFactory();
              ServletFileUpload upload = new ServletFileUpload(factory);
              try {
                  List&lt;FileItem&gt; multiparts = upload.parseRequest(request);
                  for (FileItem item : multiparts) {
                      if (!item.isFormField()) {
                          String road = "";
                          try {                           
                              String name = new File(item.getName()).getName();
                              String relativeWebPath = "/image";
                              String absoluteFilePath = request.getServletContext().getRealPath(relativeWebPath);
                              item.write(new File(absoluteFilePath, FilenameUtils.getName(item.getName())));
                              request.setCharacterEncoding("UTF-8");
                              String data = "";
                              try {
                                  data =urlcodec.decode(request.getParameter("data"), "UTF-8");
                              } catch (DecoderException e) {
                                  e.printStackTrace();
                              }
  
                          } catch (Exception e) {}
                      }
                  }
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>