在windows本地Tomcat部署项目后跑起来可以成功上传头像并显示,但是在linux上没效果
根本没有进去action,所以图片也没有保存到服务器,头像为空
<div>请上传新头像:
<form target="dis" enctype="multipart/form-data" method="post" id="uploadForm" action="${pageContext.request.contextPath}/main/uploadImg.do">
<img width="100px" style="display: none;" class="img">
<input type="file" class="imgFilea" name="file" id="file"/>
<input type="submit" class="imgFile" value="确认提交" onclick="resetPage()"/>
</form>
</div>
controller:
@RequestMapping(value="/uploadImg.do",method=RequestMethod.POST)
@ResponseBody
public String uploadImg(HttpServletRequest req,HttpSession session,MultipartFile file){
RetParam param = new RetParam();
String path = null;
try {
User user = (User) session.getAttribute("user");
if(user==null){
throw new Exception("用户不存在!");
}
if(file==null){
throw new Exception("文件读取失败!");
}
int pointValue = file.getOriginalFilename().lastIndexOf(".");
String imgType = file.getOriginalFilename().substring(pointValue+1,file.getOriginalFilename().length()); //
//判断图片类型
if(imgType.equals("png")||imgType.equals("jpg")||imgType.equals("gif")){
String name = new Date().getTime()+"."+imgType;
String headPath = "fileupload/headimg/"+name;
//保存图片到服务器
URL url = session.getServletContext().getResource("fileupload/headimg/");
path = url.getPath();
ImgUtils.saveFile(path, "text.txt","hhhh");
ImgUtils.saveImg(path,name,file);
//保存用户头像路径到数据库
UserParam userParam = new UserParam();
userParam.setId(user.getId());
userParam.setHeadPath(headPath);
userService.updateUser(userParam);
User upUser = userService.queryUserByName(userParam);
session.setAttribute("user", upUser);
param.setRetCode(RetParam.SUCCESS);
}else{
throw new Exception("图片格式错误!");
}
} catch (Exception e) {
param.setRetData(e.getMessage());
param.setRetCode(RetParam.FAULT);
try {
ImgUtils.saveFile(path, "test.txt",e.getMessage()+"");
} catch (Exception e1) {
}
}
return JSONObject.fromObject(param).toString();
}
你可以看一下保存的路径。 wondows的路径和linux的路径是不一样的。
如果都没有进去action建议查看是不是没有启动应用 或者tomcat有没有加载应用
action代码:
@RequestMapping(value="/uploadImg.do",method=RequestMethod.POST)
@ResponseBody
public String uploadImg(HttpServletRequest req,HttpSession session,MultipartFile file){
RetParam param = new RetParam();
String path = null;
try {
User user = (User) session.getAttribute("user");
if(user==null){
throw new Exception("用户不存在!");
}
if(file==null){
throw new Exception("文件读取失败!");
}
int pointValue = file.getOriginalFilename().lastIndexOf(".");
String imgType = file.getOriginalFilename().substring(pointValue+1,file.getOriginalFilename().length()); //
//判断图片类型
if(imgType.equals("png")||imgType.equals("jpg")||imgType.equals("gif")){
String name = new Date().getTime()+"."+imgType;
String headPath = "fileupload/headimg/"+name;
//保存图片到服务器
URL url = session.getServletContext().getResource("fileupload/headimg/");
path = url.getPath();
ImgUtils.saveFile(path, "text.txt","hhhh");
ImgUtils.saveImg(path,name,file);
//保存用户头像路径到数据库
UserParam userParam = new UserParam();
userParam.setId(user.getId());
userParam.setHeadPath(headPath);
userService.updateUser(userParam);
User upUser = userService.queryUserByName(userParam);
session.setAttribute("user", upUser);
param.setRetCode(RetParam.SUCCESS);
}else{
throw new Exception("图片格式错误!");
}
} catch (Exception e) {
param.setRetData(e.getMessage());
param.setRetCode(RetParam.FAULT);
try {
ImgUtils.saveFile(path, "test.txt",e.getMessage()+"");
} catch (Exception e1) {
}
}
return JSONObject.fromObject(param).toString();
}
这是页面代码,贴出来看下:
js代码:function resetPage(){
if($('.imgFilea').val()==null||$('.imgFilea').val()==''){
alert('请先选择要上传图片!');
return false;
}
var types = $('.imgFilea').val().split('.');
console.log(types[types.length-1]);
if(types[types.length-1]=="jpg"||types[types.length-1]=="png"){
alert("修改成功!");
window.location.href="${pageContext.request.contextPath}/main/toIndex.do";
}else{
alert("格式错误!");
}
}
<div>请上传新头像:
<form target="dis" enctype="multipart/form-data" method="post" id="uploadForm" action="${pageContext.request.contextPath}/main/uploadImg.do">
<img width="100px" style="display: none;" class="img">
<input type="file" class="imgFilea" name="file" id="file"/>
<input type="submit" class="imgFile" value="确认提交" onclick="resetPage()"/>
</form>
</div>