JSP如何将图片转换成二进制?
<INPUTTYPE="file"NAME="image">比如用这个查找按钮将找到的图片再通过JSP将它转换成二进制以上JSP代码是怎么写的?...
<INPUT TYPE="file" NAME="image">比如用这个查找按钮将找到的图片再通过JSP将它转换成二进制以上JSP代码是怎么写的?
展开
2013-06-01
展开全部
这以前写的一个上传下载管理系统中的资料修改功能,里面有涉及到用户图片的修改,希望对你有用,注意变量file fileCotentType fileFileName package lx;import java.io.File;import java.io.FileInputStream;
import java.io.FileOutputStream;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;import org.apache.struts2.interceptor.ServletRequestAware;import com.opensymphony.xwork2.Action;public class modify implements Action,ServletRequestAware {
private String name;
private String sex;
private String age;
private String hun;
private String address;
private String photo;
private File file;
private String fileCotentType;
private String fileFileName;
HttpServletRequest request;
HttpSession session;
public String getPhoto() {
return photo;
} public void setPhoto(String photo) {
this.photo = photo;
} public void setServletRequest(HttpServletRequest request)
{this.request=request;}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getHun() {
return hun;
}
public void setHun(String hun) {
this.hun = hun;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileCotentType() {
return fileCotentType;
}
public void setFileCotentType(String fileCotentType) {
this.fileCotentType = fileCotentType;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String execute() throws Exception
{
session=request.getSession();//获取session
ConBean cb=new ConBean();
String name=this.getName();
String sex=this.getSex();
if(sex.trim().equals("boy")) sex="男";
if(sex.trim().equals("girl"))sex="女";
String age=this.getAge();
String hun=this.getHun();
if(hun.trim().equals("yes"))hun="已婚";
if(hun.trim().equals("no"))hun="未婚";
String address=this.getAddress();
photo="imagefile/"+getFileFileName();
FileOutputStream w=new FileOutputStream("../webapps/webDemo/"+photo);
FileInputStream fis=new FileInputStream(getFile());
byte[] buffer=new byte[1024];
int len=0;
while((len=fis.read(buffer))>0)
{
w.write(buffer, 0, len);
}
String sql="update user set name='"+name+"',sex='"+sex+"',age='"+age+"',hun='"+hun+"',address='"+address+"',photo='"+photo+"' where id='"+session.getAttribute("id")+"'";
cb.con();
cb.save(sql);
cb.close();
return SUCCESS;
}
}
import java.io.FileOutputStream;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;import org.apache.struts2.interceptor.ServletRequestAware;import com.opensymphony.xwork2.Action;public class modify implements Action,ServletRequestAware {
private String name;
private String sex;
private String age;
private String hun;
private String address;
private String photo;
private File file;
private String fileCotentType;
private String fileFileName;
HttpServletRequest request;
HttpSession session;
public String getPhoto() {
return photo;
} public void setPhoto(String photo) {
this.photo = photo;
} public void setServletRequest(HttpServletRequest request)
{this.request=request;}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getHun() {
return hun;
}
public void setHun(String hun) {
this.hun = hun;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileCotentType() {
return fileCotentType;
}
public void setFileCotentType(String fileCotentType) {
this.fileCotentType = fileCotentType;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String execute() throws Exception
{
session=request.getSession();//获取session
ConBean cb=new ConBean();
String name=this.getName();
String sex=this.getSex();
if(sex.trim().equals("boy")) sex="男";
if(sex.trim().equals("girl"))sex="女";
String age=this.getAge();
String hun=this.getHun();
if(hun.trim().equals("yes"))hun="已婚";
if(hun.trim().equals("no"))hun="未婚";
String address=this.getAddress();
photo="imagefile/"+getFileFileName();
FileOutputStream w=new FileOutputStream("../webapps/webDemo/"+photo);
FileInputStream fis=new FileInputStream(getFile());
byte[] buffer=new byte[1024];
int len=0;
while((len=fis.read(buffer))>0)
{
w.write(buffer, 0, len);
}
String sql="update user set name='"+name+"',sex='"+sex+"',age='"+age+"',hun='"+hun+"',address='"+address+"',photo='"+photo+"' where id='"+session.getAttribute("id")+"'";
cb.con();
cb.save(sql);
cb.close();
return SUCCESS;
}
}
2015-12-19 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
在jsp中将图片转换成二进制通过流的方式实现的。
参考代码如下:
File file = new File("D:/IMAGE/test.jpg");
byte[] bbb = new byte[10240];
try {
InputStream a = new FileInputStream(file);
a.read(bbb);
System.out.println(bbb[0]);
System.out.println(Integer.toBinaryString(bbb[0]));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
实现思路:这里是一次把图片全读进来,所以定义的数组长度为10240,也就是10kb吧,当然你也可以定义小点,然後分次读。反正二进制都在这个数组里面,最後两句我只是举了个例子教你怎麼看这个二进制,第一个是现实二进制对应的整数,第二个是显示二进制了。
参考代码如下:
File file = new File("D:/IMAGE/test.jpg");
byte[] bbb = new byte[10240];
try {
InputStream a = new FileInputStream(file);
a.read(bbb);
System.out.println(bbb[0]);
System.out.println(Integer.toBinaryString(bbb[0]));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
实现思路:这里是一次把图片全读进来,所以定义的数组长度为10240,也就是10kb吧,当然你也可以定义小点,然後分次读。反正二进制都在这个数组里面,最後两句我只是举了个例子教你怎麼看这个二进制,第一个是现实二进制对应的整数,第二个是显示二进制了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询