当前位置:首页 » 文件管理 » java上传头像代码

java上传头像代码

发布时间: 2022-10-31 14:08:52

1. 求struts图片上传和显示

一般是这样的,上传的图片不直接放在数据库中,在数据库中只存放图片的相对路径,然后在页面的<img>元素中的src中引用这个地址就可以了。
需要上传图片的from表单必须要加上enctype="multipart/form-data"属性。
struts的文件上传bean的配置文件如下:
<form-bean name="adUpload" type="org.apache.struts.action.DynaActionForm">
<form-property name="adBewrite" type="java.lang.String"></form-property>
<form-property name="adLinkUrl" type="java.lang.String"></form-property>
<form-property name="adPosition" type="java.lang.String"></form-property>
<form-property name="adPrice" type="java.lang.String"></form-property>
<form-property name="adTimeFrom" type="java.lang.String"></form-property>
<form-property name="adTimeTo" type="java.lang.String"></form-property>
<form-property name="adPicUrl" type="org.apache.struts.upload.FormFile"></form-property>
</form-bean>
其中from-bean的type必须是org.apache.struts.action.DynaActionForm,网页中的<file>元素对应的form-bean的配置必须是:<form-property name="adPicUrl" type="org.apache.struts.upload.FormFile"></form-property>。
文件上传的java参考程序如下:
InputStream fileInput = formFile.getInputStream();
if(fileInput != null){
String path = this.getServlet().getServletContext().getRealPath("/friendFile/"+formFile.getFileName());
String path1 = "friendFile\\"+formFile.getFileName();
OutputStream outputStream = new FileOutputStream(path);
byte[] tmp = new byte[1024];
int m = 0;
while( (m = fileInput.read(tmp))!= -1) {
outputStream.write(tmp);
}
outputStream.close();
fileInput.close();

FriendLinkInfo friend = new FriendLinkInfo();
friend.setFriendName(friendName);
friend.setFriendLinkUrl(friendLink);
friend.setFriendType(friendType);
friend.setFriendImageUrl(path1);
friendDAO.addFriend(friend);
forward = mapping.findForward("flushFriend");
path用于读取文件流,path1用于表示图片在服务器上的相对路径
大致就是这样了。

2. java web实现上传头像功能,

多服务器上,存放图片的路径,是不是有区别

3. 如何在spring mvc中上传图片并显示出来

(1)在spring mvc的配置文件中配置:

<beanid="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<propertyname="uploadTempDir"value="/tmp"/><!--临时目录-->
<propertyname="maxUploadSize"value="10485760"/><!--10M-->
</bean>

(2)文件上传表单和结果展示页fileupload.jsp:

<%@pagelanguage="java"contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglibprefix="mvc"uri="http://www.springframework.org/tags/form"%>
<%@taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>SpringMVC文件上传</title>
</head>
<body>
<h2>图片文件上传</h2>
<mvc:formmodelAttribute="user"action="upload.html"
enctype="multipart/form-data">
<table>
<tr>
<td>用户名:</td>
<td><mvc:inputpath="userName"/></td>
</tr>
<tr>
<td>选择头像:</td>
<td><inputtype="file"name="file"/></td>
</tr>
<tr>
<tdcolspan="2"><inputtype="submit"value="Submit"/></td>
</tr>
</table>
</mvc:form>
<br><br>
<c:iftest="${u!=null}">
<h2>上传结果</h2>
<table>
<c:iftest="${u.userName!=null}">
<tr>
<td>用户名:</td>
<td>${u.userName}</td>
</tr>
</c:if>
<c:iftest="${u.logoSrc!=null}">
<tr>
<td>头像:</td>
<td><imgsrc="${u.logoSrc}"width="100px"height="100px"></td>
</tr>
</c:if>

</table>

</c:if>

</body>
</html>

(3)后台处理UploadController.java:

packagecn.zifangsky.controller;

importjava.io.File;
importjava.io.IOException;

importjavax.servlet.http.HttpServletRequest;

importorg.apache.commons.io.FileUtils;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
importorg.springframework.web.bind.annotation.RequestParam;
importorg.springframework.web.multipart.MultipartFile;
importorg.springframework.web.servlet.ModelAndView;

importcn.zifangsky.model.User;
importcn.zifangsky.utils.StringUtile;

@Controller
publicclassUploadController{

@RequestMapping(value="/form")
publicModelAndViewform(){
ModelAndViewmodelAndView=newModelAndView("fileupload","user",newUser());

returnmodelAndView;
}

@RequestMapping(value="/upload",method=RequestMethod.POST)
publicModelAndViewupload(Useruser,@RequestParam("file")MultipartFiletmpFile,HttpServletRequestrequest){
ModelAndViewmodelAndView=newModelAndView("fileupload");

if(tmpFile!=null){
//获取物理路径
StringtargetDirectory=request.getSession().getServletContext().getRealPath("/uploads");
StringtmpFileName=tmpFile.getOriginalFilename();//上传的文件名
intdot=tmpFileName.lastIndexOf('.');
Stringext="";//文件后缀名
if((dot>-1)&&(dot<(tmpFileName.length()-1))){
ext=tmpFileName.substring(dot+1);
}
//其他文件格式不处理
if("png".equalsIgnoreCase(ext)||"jpg".equalsIgnoreCase(ext)||"gif".equalsIgnoreCase(ext)){
//重命名上传的文件名
StringtargetFileName=StringUtile.renameFileName(tmpFileName);
//保存的新文件
Filetarget=newFile(targetDirectory,targetFileName);

try{
//保存文件
FileUtils.InputStreamToFile(tmpFile.getInputStream(),target);
}catch(IOExceptione){
e.printStackTrace();
}

Useru=newUser();
u.setUserName(user.getUserName());
u.setLogoSrc(request.getContextPath()+"/uploads/"+targetFileName);

modelAndView.addObject("u",u);
}

returnmodelAndView;
}

returnmodelAndView;
}

}

在上面的upload方法中,为了接收上传的文件,因此使用了一个MultipartFile类型的变量来接收上传的临时文件,同时为了给文件进行重命名,我调用了一个renameFileName方法,这个方法的具体内容如下:

/**
*文件重命名
*/
(StringfileName){
StringformatDate=newSimpleDateFormat("yyMMddHHmmss").format(newDate());//当前时间字符串
intrandom=newRandom().nextInt(10000);
Stringextension=fileName.substring(fileName.lastIndexOf("."));//文件后缀

returnformatDate+random+extension;
}

注:上面用到的model——User.java:

packagecn.zifangsky.model;

publicclassUser{
privateStringuserName;//用户名
privateStringlogoSrc;//头像地址

publicStringgetUserName(){
returnuserName;
}

publicvoidsetUserName(StringuserName){
this.userName=userName;
}

publicStringgetLogoSrc(){
returnlogoSrc;
}

publicvoidsetLogoSrc(StringlogoSrc){
this.logoSrc=logoSrc;
}

}

至此全部结束

效果如下:

(PS:纯手打,望采纳)

4. 在Java web网站中用户注册信息时希望用户可以选择自己的头像,然后将照片存到sql server数据库中,怎么办

用户信息表增加头像地址 字段
用文件上传的控件,文件上传到服务器指定文件夹,获取该路径,将该路径保存到数据库
登录的时候再取出来跟其他用户属性一样,显示的地方拼接到img标签里

5. java怎么生成带用户微信头像的图片,并把这张图片发送给用户。

1、下载生成二维码所需要的jar包qrcode.jar;2、直接上生成二维码的java代码 //需要导入的包import java.awt.Color;import java.awt.Graphics2D;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.File;import javax.imageio.ImageIO;import com.swetake.util.Qrcode; /** * 生成二维码(QRCode)图片 * @param content 二维码图片的内容 * @param imgPath 生成二维码图片完整的路径 * @param ccbpath 二维码图片中间的logo路径 */ public static int createQRCode(String content, String imgPath,String ccbPath) { try { Qrcode qrcodeHandler = new Qrcode(); qrcodeHandler.setQrcodeErrorCorrect('M'); qrcodeHandler.setQrcodeEncodeMode('B'); qrcodeHandler.setQrcodeVersion(7); // System.out.println(content); byte[] contentBytes = content.getBytes("gb2312"); //构造一个BufferedImage对象 设置宽、高 BufferedImage bufImg = new BufferedImage(140, 140, BufferedImage.TYPE_INT_RGB); Graphics2D gs = bufImg.createGraphics(); gs.setBackground(Color.WHITE); gs.clearRect(0, 0, 140, 140); // 设定图像颜色 > BLACK gs.setColor(Color.BLACK); // 设置偏移量 不设置可能导致解析出错 int pixoff = 2; // 输出内容 > 二维码 if (contentBytes.length > 0 && contentBytes.length < 120) { boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes); for (int i = 0; i < codeOut.length; i++) { for (int j = 0; j < codeOut.length; j++) { if (codeOut[j][i]) { gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3); } } } } else { System.err.println("QRCode content bytes length = " + contentBytes.length + " not in [ 0,120 ]. "); return -1; } Image img = ImageIO.read(new File(ccbPath));//实例化一个Image对象。 gs.drawImage(img, 55, 55, 30, 30, null); gs.dispose(); bufImg.flush(); // 生成二维码QRCode图片 File imgFile = new File(imgPath); ImageIO.write(bufImg, "png", imgFile); }catch (Exception e){ e.printStackTrace(); return -100; } return 0; }

来自网友 孤独青鸟的博客

6. 如何java中实现上传头像功能

就当文件上传啊!然后将文件存放好,将路径放入数据库!显示不就是Image 标签吗???难倒你说的是swing ??

7. 上传头像的时候报错了 java.lang.IllegalArgumentException:

这个异常的解释是"方法的参数错误",很多j2me的类库中的方法在一些情况下都会引发这样的错误,比如音量调节方法中的音量参数如果写成负数就会出现这个异常,再比如g.setcolor(int red,int green,int blue)这个方法中的三个值,如果有超过255的也会出现这个异常,因此一旦发现这个异常,我们要做的,就是赶紧去检查一下方法调用中的参数传递是不是出现了错误。

8. java问题: 当上传头像时,如何去掉背景颜色,只要头而已,这应该怎么弄,用到什么技术,求问

那你要在上传的时候先用PS处理一下这个照片的背景色.....程序好像不具备这能力..

9. JSP上传图片做头像报错 系统找不到指定文件

肯定找不到啊,你运行的时候获取的是tomcat容器的目录啊,但是你的文件却在其他的盘或文件下,肯定不对的,要将文件复制到tomcat容器里面去!!

10. java选择头像的代码

<select name="tupian">
<option value="头像1.jpg">头像1</option>
<option value="头像2.jpg">头像2</option>
</select>

热点内容
手机点菜app怎么连接电脑服务器 发布:2025-07-05 11:13:05 浏览:942
配置控制台干什么用的 发布:2025-07-05 10:54:51 浏览:961
连信从哪里改登录密码 发布:2025-07-05 10:54:12 浏览:398
怎么修改查询密码 发布:2025-07-05 10:49:48 浏览:966
matlab文件存储 发布:2025-07-05 10:40:46 浏览:85
梅州市用工实名制管理平台云存储 发布:2025-07-05 10:28:59 浏览:77
安卓origin怎么设置 发布:2025-07-05 10:20:10 浏览:542
安卓为什么跳水 发布:2025-07-05 09:55:08 浏览:88
达内学校php 发布:2025-07-05 09:52:05 浏览:399
获取数据库所有表 发布:2025-07-05 09:39:12 浏览:655