简单使用fileupload上传图片

上传使用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="上传" οnclick="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>
 
 
 自己用来做笔记的

转载于:https://www.cnblogs.com/Dream--/p/7465076.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值