package com.meng.upload;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
@Controller
public class upload {
@RequestMapping(value="/upload",method=RequestMethod.POST)
public String upload(@RequestParam("file1") CommonsMultipartFile[] file1, HttpServletRequest request) {
for (CommonsMultipartFile commonsMultipartFile : file1) {
try {
File file = new File(request.getServletContext().getRealPath("upload"),
System.currentTimeMillis() + "_"
+ commonsMultipartFile.getOriginalFilename());
System.out.println(file.getPath()+"");
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(commonsMultipartFile.getBytes());
fileOutputStream.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "ok";
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<script type="text/javascript">
//判断浏览器是否支持FileReader接口
if (typeof FileReader == 'undefined') {
alert("<h1>当前浏览器不支持FileReader接口</h1>");
}
//选择图片,马上预览
function xmTanUploadImg(obj) {
var file = obj.files[0];
var reader = new FileReader();
reader.onload = function(e) {
var img = document.getElementById("img1");
img.src = e.target.result;
}
reader.readAsDataURL(file);
}
</script>
</head>
<body>
<form action="upload.upload" method="post" enctype="multipart/form-data">
file1::<input type="file" name="file1" accept=".jpg,.png" onchange="xmTanUploadImg(this)" />
<input type="submit" />
<img id="img1" />
</form>
</body>
</html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有