使用spring mvc将图像上传到jboss服务器部署tmp文件夹 [英] image uploading using spring mvc to jboss server deployment tmp folder

查看:70
本文介绍了使用spring mvc将图像上传到jboss服务器部署tmp文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目基于Maven多模块项目.

hi my project is based on maven multi module project.

项目结构如下. 即时通讯使用jboss 7作为我的服务器.

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

Response
ResponseCommons
ResponseEar
ResponseModel
ResponseService
ResponseWeb

,我完成了一张图片上传表格.上传工作正常,并且图像已上传到 web 模块的资源文件夹中. 问题是图像正在上传到jboss服务器的 tmp 文件夹中,我如何更改为 ResponseWeb/webapp/resources/css/图像名称.

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.

当前图像保存位置

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

我使用ajax表单提交上传图片. 用于上传图片的控制器

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>

推荐答案

要保存文件的位置实际上是无效的,实际上是在WAR文件中,该文件分解到JBoss的temp目录中,因此您请参阅.../tmp/vfs/deployment ....文件夹.

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. 如果使用的是Servlet 3.0,则可以将多部分servlet配置为注释或在web.xml中.您可以通过以下方式注释纯servlet.

@WebServlet("/myImageFileUploader")
@MultipartConfig(location = "/opt/myImageFileUploadLocation")
public class MyImageFileUploaderServlet extends HttpServlet {
.....}

XML配置如下

  <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>

如果使用的是Servlet 3.0,则上述XML示例可以直接在您的项目中使用.为了方便起见,示例代码正在配置Spring DispatcherServlet.默认情况下,JBoss 7 Web具有Servlet 3.0,所以我想它可以工作.

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. 如果您的Servlet版本是3.0之前的版本,那么您可以在spring配置文件中配置commons-fileupload,如下所示.

<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>

希望这会有所帮助.

这篇关于使用spring mvc将图像上传到jboss服务器部署tmp文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆