上传图片
来源:互联网 发布:杨千嬅 唱功 知乎 编辑:程序博客网 时间:2024/06/07 04:43
controller:
//上传新闻图片
@RequestMapping("/sendUp")
@ResponseBody
public Map<String, Object> sendUp(@RequestParam(value = "newsImage", required = false) MultipartFile fileImg, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, Object> map = new HashMap<>();
String realPath = request.getSession().getServletContext().getRealPath("/");
response.setContentType("text/plain;charset=UTF-8");
String fileName = null;
if (fileImg.isEmpty()) {
} else {
try {
fileName = fileImg.getOriginalFilename();
String trueFileName = String.valueOf(System.currentTimeMillis() + fileName);
String wlPath = "C:\\Users\\u\\Desktop\\just_do_it\\news_project\\src\\main\\resources\\static\\img\\" + trueFileName;
String xdPath = "/img/" + trueFileName;
String tempPath = "D:\\java\\xiangmu\\news_project\\target\\classes\\static\\img\\" + trueFileName;
System.out.print(xdPath);
session.setAttribute("xdPath", xdPath);
org.apache.commons.io.FileUtils.copyInputStreamToFile(fileImg.getInputStream(), new File(wlPath));
org.apache.commons.io.FileUtils.copyInputStreamToFile(fileImg.getInputStream(), new File(tempPath));
map.put("xdPath", xdPath);
} catch (IOException e) {
}
}
return map;
}
jsp:
body:
<p>文件:<input type="file" name="newsImage" id="file1"/></p>
<p><img id="img1" th:src="@{/img/15052077692461-1309231H611.jpg}"/></p>
<p><input type="button" value="上传" id="upimg"/></p>
js:
<script type="text/javascript">
$(function () {
$("#upimg").click(function () {
if ($("#file1").val().length > 0) {
FileUpload();
}
else {
alert("请选择图片");
}
})
})
function FileUpload() {
var str;
$.ajaxFileUpload({
type: "post",
url: "/news/sendUp", //用于文件上传的服务器端请求地址
fileElementId: "file1", //文件上传空间的id属性 <input type="file" id="file" name="file" />
dataType: "json", //返回值类型 一般设置为json
success: function (data) //服务器成功响应处理函数
{
alert("上传成功")
str = data.xdPath;
$("#img1").attr("src", str);
},
error: function ()//服务器响应失败处理函数
{
alert("上传失败");
}
});
}
</script>
别忘记jsp页面引用upload的jar
//上传新闻图片
@RequestMapping("/sendUp")
@ResponseBody
public Map<String, Object> sendUp(@RequestParam(value = "newsImage", required = false) MultipartFile fileImg, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, Object> map = new HashMap<>();
String realPath = request.getSession().getServletContext().getRealPath("/");
response.setContentType("text/plain;charset=UTF-8");
String fileName = null;
if (fileImg.isEmpty()) {
} else {
try {
fileName = fileImg.getOriginalFilename();
String trueFileName = String.valueOf(System.currentTimeMillis() + fileName);
String wlPath = "C:\\Users\\u\\Desktop\\just_do_it\\news_project\\src\\main\\resources\\static\\img\\" + trueFileName;
String xdPath = "/img/" + trueFileName;
String tempPath = "D:\\java\\xiangmu\\news_project\\target\\classes\\static\\img\\" + trueFileName;
System.out.print(xdPath);
session.setAttribute("xdPath", xdPath);
org.apache.commons.io.FileUtils.copyInputStreamToFile(fileImg.getInputStream(), new File(wlPath));
org.apache.commons.io.FileUtils.copyInputStreamToFile(fileImg.getInputStream(), new File(tempPath));
map.put("xdPath", xdPath);
} catch (IOException e) {
}
}
return map;
}
jsp:
body:
<p>文件:<input type="file" name="newsImage" id="file1"/></p>
<p><img id="img1" th:src="@{/img/15052077692461-1309231H611.jpg}"/></p>
<p><input type="button" value="上传" id="upimg"/></p>
js:
<script type="text/javascript">
$(function () {
$("#upimg").click(function () {
if ($("#file1").val().length > 0) {
FileUpload();
}
else {
alert("请选择图片");
}
})
})
function FileUpload() {
var str;
$.ajaxFileUpload({
type: "post",
url: "/news/sendUp", //用于文件上传的服务器端请求地址
fileElementId: "file1", //文件上传空间的id属性 <input type="file" id="file" name="file" />
dataType: "json", //返回值类型 一般设置为json
success: function (data) //服务器成功响应处理函数
{
alert("上传成功")
str = data.xdPath;
$("#img1").attr("src", str);
},
error: function ()//服务器响应失败处理函数
{
alert("上传失败");
}
});
}
</script>
别忘记jsp页面引用upload的jar
阅读全文
0 0
- 图片上传
- 上传图片
- 上传图片
- 上传图片
- 上传图片
- 上传图片
- 图片上传
- 图片上传
- 上传图片
- 上传图片
- 图片上传
- 图片上传
- 图片上传
- 图片上传
- 上传图片
- 上传图片
- 上传图片
- 图片上传
- C++中重载、重写、重定义
- java 多线程随记
- SwipyRefreshLayout+recyclerview
- TCP和UDP的最完整的区别
- repo的介绍
- 上传图片
- B-Tree / B+Tree 结构以及MySQL索引
- 【知了堂学习笔记】myBatis实现增删查改
- linux:破解navicat
- Tablayout
- Linux:mysql数据库的热备份,主从数据库搭建
- java集合list
- Hash Table -- Leetcode problem349. Intersection of Two Arrays
- PHP实现阶乘的原理与代码分析