image uploading using spring mvc to jboss server d

2019-06-08 07:40发布

hi my project is based on maven multi module project.

project structure is given below. im using jboss 7 as my server.

Response
ResponseCommons
ResponseEar
ResponseModel
ResponseService
ResponseWeb

and im done a image upload form. uploading working fine and the image is uploaded to the resource folder of web module. The problem is the image is uploading to tmp folder of the jboss server ,how can i changes to ResponseWeb/webapp/resources/css/ image name.

current image saving location

C:\jboss-as-7.1.1.Final\stand
alone\tmp\vfs\deploymenteec45ba06bd34543\ResponseWeb-1.2-SNAPSHOT.war-295
28a7e2cc4df5e\resources\css

im using ajax form submit to upload image. controller for uploading image

@RequestMapping(value = "/uploadImage.html", method = RequestMethod.POST)
    @ResponseBody
    public String uploadImageTest(@RequestParam("demoImage") MultipartFile file) throws IllegalStateException, IOException {
        try {
            String fileName = null;
            InputStream inputStream = null;
            OutputStream outputStream = null;
            if (file.getSize() > 0) {
                inputStream = file.getInputStream();

                System.out.println("File Size:::" + file.getSize());

                System.out.println("size::" + file.getSize());
                fileName = request.getServletContext().getRealPath("/resources/") + "/css/"
                        + file.getOriginalFilename();
                outputStream = new FileOutputStream(fileName);
                System.out.println("fileName:" + file.getOriginalFilename());
                int readBytes = 0;
                byte[] buffer = new byte[10000];
                while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) {
                    outputStream.write(buffer, 0, readBytes);
                }
                outputStream.close();
                inputStream.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "saved";
    }

HTML

<form th:action="@{/school-admin/uploadImage.html}"
                        id="imageUploadForm" method="post" enctype="multipart/form-data">
                        <div class="row">
                            <div class="col-lg-2" style="margin-bottom: -40px;">
                                <div class="thumbnail">
                                    <img id="imgStud" th:src="@{/resources/img/profile.png}"
                                        style="width: 172px; height: 198px;" /> <br /> <input
                                        type="file" accept="image/*" name="demoImage" id="demoImage"
                                        onchange="fileSelected();" style="width: 170px;" />

                                </div>
                                <br />
                            </div>
                        </div>
                        <input type="button" class="btn btn-info pull-right"
                            id="btnUpload" value="upload" />
                    </form>

1条回答
成全新的幸福
2楼-- · 2019-06-08 07:54

The location you want to save the file is invalid, as it is in fact, inside your WAR file, which gets exploded into the JBoss temp directory, hence you see the .../tmp/vfs/deployment.... folder.

However, you can specify a particular location for your default multi-part upload location, in multiple ways.

  1. If you are using Servlet 3.0, you configure multipart servlet, either as annotation or in web.xml. You can annotate a pure servlet, in the following way.
@WebServlet("/myImageFileUploader")
@MultipartConfig(location = "/opt/myImageFileUploadLocation")
public class MyImageFileUploaderServlet extends HttpServlet {
.....}

XML configuration is as below

  <servlet>
    <servlet-name>MySpringDispatcher1</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:/spring-servlet.xml</param-value>
    </init-param>
    <multipart-config>
        <location>/opt/myImageFileUploadLocation</location>
        <max-file-size>52428800</max-file-size>
        <max-request-size>52428800</max-request-size>
        <file-size-threshold>0</file-size-threshold>
    </multipart-config>
</servlet>

The above XML example can be used directly in your project, if you are using Servlet 3.0. The sample code is configuring the Spring DispatcherServlet, for your convenience. JBoss 7 Web has Servlet 3.0, by default, so I guess it will work.

  1. If your Servlet version is pre 3.0, I mean, older versions, then you can configure commons-fileupload in the spring configuration file, as given below.
<bean id="myImageMultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  <property name="maxUploadSize" value="100000" />
  <property name="uploadTempDir" ref="myImageFileUploadDirResource" />
</bean>

<bean id="myImageFileUploadDirResource" class="org.springframework.core.io.FileSystemResource">
  <constructor-arg>
  <value>/opt/myImageFileUploadLocation</value>
  </constructor-arg>
</bean>

Hope this helps.

查看更多
登录 后发表回答