java头像上传求代码
新手一个正在用java做网站,希望哪位大神有源代码,教教小弟就剩下这点分了,希望大神们行行好,看见了帮助小弟一下...
新手一个正在用java做网站,希望哪位大神有源代码,教教小弟
就剩下这点分了,希望大神们行行好,看见了帮助小弟一下 展开
就剩下这点分了,希望大神们行行好,看见了帮助小弟一下 展开
2014-12-17
展开全部
你看看这个,使我们项目用到的
/**
* 上传图片
*/
@RequestMapping(value="/reviseTuPian",method=RequestMethod.POST)
@ExceptionHandler(Exception.class)
public String reviseTuPian(Model model,HttpServletResponse response,HttpServletRequest request,Exception ex){
//获取session
UserInfos info = (UserInfos) request.getSession().getAttribute("user");
/空败毁/保存对象值
Map<String, String>枯滚 parameterMap = new HashMap<String, String>();
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload fileUpload = new ServletFileUpload(factory);
fileUpload.setFileSizeMax(1024 * 200);
String filename=null;
try {
List<FileItem> fileItems = fileUpload.parseRequest(request);
for (FileItem fileItem : fileItems) {
if (fileItem.isFormField()){
//不是上传文件
String description = fileItem.getFieldName();
String value = fileItem.getString("utf-8");//处理乱斗备码
parameterMap.put(description, value);
}else {
filename = fileItem.getName();
if (filename == null || filename.trim().length() == 0) {
parameterMap.put("imgurl", null);
}else{
filename = UploadUtils.subFileName(filename);
String uuidname = UploadUtils.generateRandonFileName(filename);
String dir = UploadUtils.generateRandomDir(uuidname);
File dirFile = new File(request.getSession().getServletContext().getRealPath(
"/touxiang" + dir));
dirFile.mkdirs();//创建目录
InputStream in = new BufferedInputStream(fileItem.getInputStream());
OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(dirFile,uuidname)));
int b;
while((b = in.read())!=-1){
out.write(b);
}
in.close();
out.close();
//删除文件
fileItem.delete();
// 生成小图
PicUtils picUtils = new PicUtils(dirFile.getCanonicalPath()
+ "/" + uuidname);
picUtils.resize(150, 150);
//获取小图路径
String img_s = UploadUtils.getImg_s(uuidname);
//System.out.println("qfeer图片名称qwfwdfq"+img_s);
parameterMap.put("imgurl", "/touxiang"+dir+"/"+img_s);
}
}
}
} catch (FileSizeLimitExceededException e) {
model.addAttribute("asg", "文件大小不得超过100k");
e.printStackTrace();
//request.setAttribute("asg", "文件大小不得超过100k");
return "redirect:/reviseUser.do";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(filename!=""){ //上传图片是否为空
String[] m=filename.split("\\.");
String[] n={"BMP","JPG","JPEG","PNG","GIF"};
for (int i = 0; i < n.length; i++) {
if(m[1].equalsIgnoreCase(n[i])){
//添加数据
info.setHeadImageUrl(parameterMap.get("imgurl"));
//调用业务层
ufil.updateNiChengUser(info);
model.addAttribute("asg", "头像更新成功");
System.out.println("*************上传保存路径*****************"+request.getSession().getServletContext().getRealPath("/"));
break;
}else{
System.out.println("头像更新失败");
model.addAttribute("asg", "图片格式不正确,请重新选择");
}
} //循环结束
}else{ //判断结束图片为空时候
//添加数据
info.setHeadImageUrl(parameterMap.get("imgurl"));
//调用业务层
ufil.updateNiChengUser(info);
System.out.println("*************上传保存路径*****************"+request.getSession().getServletContext().getRealPath("/"));
model.addAttribute("asg", "图片更新成功");
}
return "redirect:/reviseUser.do";
}
/**
* 上传图片
*/
@RequestMapping(value="/reviseTuPian",method=RequestMethod.POST)
@ExceptionHandler(Exception.class)
public String reviseTuPian(Model model,HttpServletResponse response,HttpServletRequest request,Exception ex){
//获取session
UserInfos info = (UserInfos) request.getSession().getAttribute("user");
/空败毁/保存对象值
Map<String, String>枯滚 parameterMap = new HashMap<String, String>();
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload fileUpload = new ServletFileUpload(factory);
fileUpload.setFileSizeMax(1024 * 200);
String filename=null;
try {
List<FileItem> fileItems = fileUpload.parseRequest(request);
for (FileItem fileItem : fileItems) {
if (fileItem.isFormField()){
//不是上传文件
String description = fileItem.getFieldName();
String value = fileItem.getString("utf-8");//处理乱斗备码
parameterMap.put(description, value);
}else {
filename = fileItem.getName();
if (filename == null || filename.trim().length() == 0) {
parameterMap.put("imgurl", null);
}else{
filename = UploadUtils.subFileName(filename);
String uuidname = UploadUtils.generateRandonFileName(filename);
String dir = UploadUtils.generateRandomDir(uuidname);
File dirFile = new File(request.getSession().getServletContext().getRealPath(
"/touxiang" + dir));
dirFile.mkdirs();//创建目录
InputStream in = new BufferedInputStream(fileItem.getInputStream());
OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(dirFile,uuidname)));
int b;
while((b = in.read())!=-1){
out.write(b);
}
in.close();
out.close();
//删除文件
fileItem.delete();
// 生成小图
PicUtils picUtils = new PicUtils(dirFile.getCanonicalPath()
+ "/" + uuidname);
picUtils.resize(150, 150);
//获取小图路径
String img_s = UploadUtils.getImg_s(uuidname);
//System.out.println("qfeer图片名称qwfwdfq"+img_s);
parameterMap.put("imgurl", "/touxiang"+dir+"/"+img_s);
}
}
}
} catch (FileSizeLimitExceededException e) {
model.addAttribute("asg", "文件大小不得超过100k");
e.printStackTrace();
//request.setAttribute("asg", "文件大小不得超过100k");
return "redirect:/reviseUser.do";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(filename!=""){ //上传图片是否为空
String[] m=filename.split("\\.");
String[] n={"BMP","JPG","JPEG","PNG","GIF"};
for (int i = 0; i < n.length; i++) {
if(m[1].equalsIgnoreCase(n[i])){
//添加数据
info.setHeadImageUrl(parameterMap.get("imgurl"));
//调用业务层
ufil.updateNiChengUser(info);
model.addAttribute("asg", "头像更新成功");
System.out.println("*************上传保存路径*****************"+request.getSession().getServletContext().getRealPath("/"));
break;
}else{
System.out.println("头像更新失败");
model.addAttribute("asg", "图片格式不正确,请重新选择");
}
} //循环结束
}else{ //判断结束图片为空时候
//添加数据
info.setHeadImageUrl(parameterMap.get("imgurl"));
//调用业务层
ufil.updateNiChengUser(info);
System.out.println("*************上传保存路径*****************"+request.getSession().getServletContext().getRealPath("/"));
model.addAttribute("asg", "图片更新成功");
}
return "redirect:/reviseUser.do";
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你搜一下java图片上传就可以~
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
网上很多 的 想要做好 你得学好CSS js
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询