2、其次分配好电脑上传路径后,创建一个迭代器。
最后通过迭代器进行判空,再通过循环来实现多个文件的上传悉带,再输出文件信息的同时上传多个系物液统即可。
java中是可以支持多文件上传的,参考代码如下:
@RequestMapping(value = {"multipleFileUpload"}, method = {RequestMethod.GET, RequestMethod.POST})public String multipleFileUpload(
ModelMap modelMap,
MultipartHttpServletRequest request,
HttpServletResponse response,
@RequestParam(value = "type") String type,
@RequestParam(value = "jobId") String jobId) throws IOException {
List < MultipartFile > files = request.getFiles("files")
response.setHeader("Access-Control-Allow-Origin", "http://www.gifmiao.com")
Map<String, Object> statusMap = new HashMap<>()
HttpSession session = request.getSession()
session.setAttribute("gifCompressStatus", statusMap)
int compressSize = getCompressSizeByValue(type)
for(MultipartFile file :files){
搭高 String filename = file.getOriginalFilename().split(".gif")[0]
Map<String, Object> resultMap = new HashMap<>()
resultMap.put("size" , 0)
resultMap.put("status" , 0)
昌悔 resultMap.put("url" , "")
statusMap.put(filename, resultMap)
InputStream is = file.getInputStream()
byte[] bytes = IOUtils.toByteArray(is)
CompressWorker worker = new CompressWorker(statusMap, bytes, filename, compressSize, jobId)
worker.start()
}
modelMap.addAttribute("json", StringUtils.toInsensitiveJson(new ReturnMap("线程已启动")))
耐枝正 return "json"
}
刚学完多文件上传,希望对你有帮助。多文件上传,就是把多个文件一起传携岁到指定的位置激兆,他和单文件上传类似,只需要改动几个地方而已即可。首先是上传页面,由于要上传多个辩铅睁文件,所以就必须有多个供用户选择的文件框,然后修改Action,把action中属性的类型修改为list。
action如下:
public class UploadActionMany extends ActionSupport{
private List<File>upload
private List<String>uploadFileName
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.print(upload.size())
if(upload!=null){
for (int i = 0i <upload.size()i++) {
InputStream is=new FileInputStream(upload.get(i)) OutputStream os=new FileOutputStream("d:\\upload\\"+getUploadFileName().get(i)) byte buffer[]=new byte[1024]
int count=0
while((count=is.read(buffer))>0){
os.write(buffer,0,count)
}
os.close()
is.close()
}
}
return SUCCESS
}
省略set和get方法
jsp如下:
<html>
<head>
<title>文件上传</title>
</head>
<body>
<s:form action="upload.action" method="post" enctype="multipart/form-data"> <s:file name="upload" label="上传的文件一"></s:file>
<s:file name="upload" label="上传的文件二"></s:file>
<s:file name="upload" label="上传的文件三"></s:file>
<s:submit value="上传"></s:submit>
</s:form>
</body>
</html>
希望对你有帮助,打代码不容易啊,记得给点分
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)