Store a pdf into the application file and extract

2019-09-01 04:19发布

问题:

Hallo i manage to create a form which gives me the chance to upload a file. The code is the following:

applicationContext.xml:

  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="100000"/>
 </bean>

The form:

 <%@page contentType="text/html;charset=UTF-8" %>
 <%@page pageEncoding="UTF-8" %>
 <%@ page session="false" %>
 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<html>
 <head>
     <META http-equiv="Content-Type" content="text/html;charset=UTF-8">
     <title>Upload Example</title>
 </head>
 <body>
    <form:form modelAttribute="uploadItem" method="post" enctype="multipart/form-data">
        <fieldset>
            <legend>Upload Fields</legend>

            <p>
                <form:label for="name" path="name">Name</form:label><br/>
                <form:input path="name"/>
            </p>

            <p>
                <form:label for="fileData" path="fileData">File</form:label><br/>
                <form:input path="fileData" type="file"/>
            </p>

            <p>
                <input type="submit" />
            </p>

        </fieldset>
    </form:form>
  </body>
</html>

the model:

public class UploadItem
{
private String name;
private CommonsMultipartFile fileData;

public String getName()
{
  return name;
}

public void setName(String name)
{
  this.name = name;
}

public CommonsMultipartFile getFileData()
{
  return fileData;
}

public void setFileData(CommonsMultipartFile fileData)
{
 this.fileData = fileData;
}

}

the controller:

@Controller
@RequestMapping(value = "/upload")
public class UploadController
{
@RequestMapping(method = RequestMethod.GET)
public String getUploadForm(Model model)
{
  model.addAttribute(new UploadItem());
  return "upload/uploadForm";
}

@RequestMapping(method = RequestMethod.POST)
public String create(UploadItem uploadItem, BindingResult result)
{
if (result.hasErrors())
{
  for(ObjectError error : result.getAllErrors())
  {
    System.err.println("Error: " + error.getCode() +  " - " + error.getDefaultMessage());
  }
  return "upload/uploadForm";
}


return "redirect:/app/";

} }

Now my question is how can i store the upload file into a folder in the application.I want to have as name the name gived by the user at the textbox.and also how can i extract and open the pdf file to a browser?What should i add in the existing code?I don't have an idea how can i do this.Please help me

回答1:

You just need to add one more parameter to your controller method like HttpServletRequest request.

After that you need to get the real path in the method with below mentioned line.

request.getServletContext().getRealPath("your directory name here"); 

you need to create a directory in the application root folder. And replace the name in abve line.

Then create a File object with the real path you get. After that please write below line.

uploadItem.getFileDate.transferTo(you file object created above);

And you are done. I think this should do the work for you.

Hope that helps you.

Cheers.



回答2:

If you will specify another name for the file then it will not replace the existing file. If the file name will be same then it will replace for sure. Very Obvious.

Secdond thing is when you define produces={"application/pdf"}, you also need to make your jsp page having this content-type.