上传使用jar:
<dependency>
???<groupId>commons-fileupload</groupId>
???<artifactId>commons-fileupload</artifactId>
???<version>1.3.1</version>
</dependency>
超链接传bookI
<a href="/jsp/manageuser/imgupload.jsp?bookId=${li.id}">上传封面</a>
页面取出bookId
String bookId = request.getParameter("bookId");
页面:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%
???String path = request.getContextPath();
???String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;
???String bookId = request.getParameter("bookId");
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
???<meta charset="utf-8">
???<meta http-equiv="X-UA-Compatible" content="IE=edge">
???<meta name="viewport" content="width=device-width, initial-scale=1">
???<title>test</title>
???<!-- Bootstrap -->
???<script src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
???<script src="/js/ajaxfileupload.js"></script>
</head>
<body>
<script type="text/javascript">
???function up() {
???????$.ajaxFileUpload({
???????????type: "POST",
???????????url: ‘/manage/sendUp.action?bookId=<%=bookId%>‘,
???????????secureuri: false,
???????????fileElementId: ‘fileImg‘,//file标签的id
???????????dataType: ‘text‘,//返回数据的类型
???????????success: function (data, status) {
???????????????alert("成功");
???????????????window.location.href = "<%=basePath%>/manage/showBook.action";
???????????????//window.location.href = "<%=basePath%>/manage/toUpdateBook.action";
???????????},
???????????error: function () {
???????????????alert("异常");
???????????}
???????});
???}
</script>
<input type="file" id="fileImg" name="fileImg">
<input type="button" value="上传" onclick="up()">
<input type="hidden" id="bookId" name="bookId" value="${id}">
</body>
</html>
controller:
//头像上传
@RequestMapping(value = "/sendUp", method = RequestMethod.POST)
@ResponseBody
public String sendUp(@RequestParam("bookId") int bookId, @RequestParam MultipartFile[] fileImg, HttpServletRequest request, HttpServletResponse response) throws IOException {
???Map<String, Object> map = new HashMap<>();
??/* map.put("code", 1);
???System.out.print("收到用户[" + bookId + "]的文件上传请求");*/
???//文件实际上传路径
???String realPath = request.getSession().getServletContext().getRealPath("/");
???response.setContentType("text/plain;charset=UTF-8");
???//设置响应给前台的PrintWriter对象
?/* ?PrintWriter out = response.getWriter();*/
???String fileName = null;
???for (MultipartFile myfile : fileImg) {
???????if (myfile.isEmpty()) {
???????????/*out.print(map.toString());
???????????out.flush();*/
???????} else {
???????????try {
???????????????fileName = myfile.getOriginalFilename();
???????????????String trueFileName = String.valueOf(System.currentTimeMillis() + fileName);
???????????????String wlPath = "D:\\java\\xiangmu\\first_project\\web\\resources\\img\\" + trueFileName;
???????????????String xdPath = "../resources/img/" + trueFileName;
???????????????String tempPath = realPath + "resources\\img\\" + trueFileName;
???????????????map.put("bookId", bookId);
???????????????map.put("xdPath", xdPath);
???????????????isService.updateImg(map);
???????????????FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(wlPath));
???????????????FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(tempPath));
??????????????/* map.put("code", 0);*/
????????????/* ??out.print(map.toString());
???????????????out.flush();
???????????????int count = 1;
???????????????map.put("count", count);*/
???????????} catch (IOException e) {
?????????????/* ?e.printStackTrace();
???????????????out.print(map.toString());
???????????????out.flush();*/
???????????}
???????}
???}
???return "1";
}
img文件夹位置
spring-mvc。xml:
设置上传文件大小
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
???<property name="maxUploadSize" value="10485760"/>
</bean>
自己用来做笔记的
简单使用fileupload上传图片
原文地址:http://www.cnblogs.com/Dream--/p/7465076.html