0

我一直在开发一个 Spring 应用程序,我在上传文件部分遇到了一些问题。在第一种情况下,我只有一个文件输入类型,它工作正常,但知道我已经放置了一个添加更多输入的 javascript,但我真的不知道如何在控制器中处理它。

我的 JSP 看起来像:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page session="false" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %> 
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="<c:url value="/resources/css/form-upload.css" />" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width">
    <link rel="stylesheet" href="http://blueimp.github.com/cdn/css/bootstrap.min.css" />
    <script type="text/javascript" src="<c:url value="/resources/js/css_browser_selector.js"/>"></script>
<script type="text/javascript">
/** 
  A lot of code that make the "magic" of adding one field
*/
</script> 
</head>
<body style="background: white;">
<c:url var="subeUrl" value="/process/doUpload" />
    <form name="file1" method="POST" action="${subeUrl}" enctype="multipart/form-data" class="formulario"> 
        <div id="titulo"><p>Adjuntar archivos</p></div></br>
        <fieldset id="upload-message">
            <table>
                <tbody id="color_contacts">
                     <tr valign="top">
                        <td colspan="5" id="ancho">
                            <div class="file-wrapper">
                                <input type="file" name="filename[0]" id="filename[0]" size="9" value="" />
                                <input name="id_solicitud" type="hidden" value="${id}" />
                                <div id="hideSpan"><span class="buttones">Choose a file</span></div>
                            </div>
                            <div class="for-same">
                                <div class="explorer">
                                    <button onclick="javascript:addColorControl();return false;" class="btn btn-success fileinput-button">
                                    <i class="icon-plus icon-white"></i>
                                    </button>
                                </div>
                            </div>
                        </td>
                     </tr>
                </tbody>
            </table>
            </br>
            <div class="post-form"><input type="submit" value="Subir" name="submit" class="post-form" /></div>
        </fieldset>
    </form>
</body>
</html>

如您所见,表格中的文件名是“filename [index]”。

在我的控制器中,我尝试了这个,但你可以想象,它没有用:

@RequestMapping(value="/doUpload", method=RequestMethod.POST)
    public String doUpload(HttpServletRequest req, HttpSession session, @RequestParam("filename[]") CommonsMultipartFile[] filename, Map<String, Object> map) throws Exception{

        String cod = (String) session.getAttribute("user");

        File folder = new File("C:/wamp/www/Uploads/"+ cod + "/");
        folder.mkdirs();

        Archivo arc = new Archivo();

        int a = 0;
        if (filename[a] != null && filename[a].getSize() > 0 && a < 4) {
            for (CommonsMultipartFile aFile : filename){
                arc.setId_archivo("0");
                arc.setId_solicitud(req.getParameter("id_solicitud"));

                System.out.println("Saving file: " + saveDirectory + cod + "/" + aFile.getOriginalFilename());

                if (!aFile.getOriginalFilename().equals("")) {
                    aFile.transferTo(new File(saveDirectory + cod + "/" + aFile.getOriginalFilename()));
                }
                    if (aFile.getOriginalFilename() != ""){
                    String nombreArch = "http://localhost/Uploads/direct_download.php?file=" +cod + "/" + aFile.getOriginalFilename();
                    System.out.println(nombreArch);
                    arc.setArchivo(nombreArch); 
                }
                else {
                    arc.setArchivo(null);
                }
                    a ++;
            }
            service.agregaFile(arc);        
        }

        return "cliente/successUp";
    }

我希望有人能帮助我,我是春天的新手。提前致谢。:)

4

2 回答 2

0

好吧,我正在回答我自己的问题,最后真的很简单。我只是将输入的名称更改为,filename因此所有输入都具有相同的名称,并且在我的控制器中我更改了这个:

@RequestMapping(value="/doUpload", method=RequestMethod.POST)
    public String doUpload(HttpServletRequest req, HttpSession session, @RequestParam CommonsMultipartFile[] filename, Map<String, Object> map) throws Exception{

    String cod = (String) session.getAttribute("user");

    File folder = new File("C:/wamp/www/Uploads/"+ cod + "/");
    folder.mkdirs();

    Archivo arc = new Archivo();


    if (filename != null && filename.getSize() > 0 ) {
        for (CommonsMultipartFile aFile : filename){
            arc.setId_archivo("0");
            arc.setId_solicitud(req.getParameter("id_solicitud"));

            System.out.println("Saving file: " + saveDirectory + cod + "/" + aFile.getOriginalFilename());

            if (!aFile.getOriginalFilename().equals("")) {
                aFile.transferTo(new File(saveDirectory + cod + "/" + aFile.getOriginalFilename()));
            }
                if (aFile.getOriginalFilename() != ""){
                String nombreArch = "http://localhost/Uploads/direct_download.php?file=" +cod + "/" + aFile.getOriginalFilename();
                System.out.println(nombreArch);
                arc.setArchivo(nombreArch); 
            }
            else {
                arc.setArchivo(null);
            }
               service.agregaFile(arc); 
        }

    }

刚刚删除了filename[]CommonsMultiPartFile @RequestParam 中的部分,service.agregaFile(arc)并将for. 现在一切正常!不管怎么说,还是要谢谢你。

于 2013-06-11T17:22:26.077 回答
0

我不确定您是否可以在处理程序方法参数上注入 CommonsMultipartFile,请尝试使用 org.springframework.web.multipart.MultipartFile。

还要确保在你的 spring 容器上定义了一个 CommonsMultipartResolver,如下所示:

<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

并且类路径中包含 commons-fileupload jar(即:作为 maven 依赖项)

于 2013-06-10T23:04:09.993 回答