我一直在开发一个 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";
}
我希望有人能帮助我,我是春天的新手。提前致谢。:)