JSP实现图片上传代码
<title>upload.jsp</title>
<%
String path = request.getContextPath();
%>
<body>
<html:errors/>
<form action="upld.do" method="post">
<img id="image" name="image" src="<%=path %>/W.JPG" width="100"/>
<input id="file" type="file" name="file" οnchange="setImage()"/>
<input type="submit" value="上传">
</form>
<script language="javascript">
function setImage()
{
document.getElementById("image").src = document.getElementById("file").value;
}
</script>
</body>
<title>img.jsp</title>
<%
HttpSession se = request.getSession();
Integer m = Integer.parseInt(se.getAttribute("m").toString());
%>
<body>
<%
String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/user";
String username = "root";
String password = "123456";
Connection con;
PreparedStatement pst = null;
ResultSet rs;
byte bu[] = new byte[m];
Class.forName(driverName);
con = DriverManager.getConnection(url, username, password);
pst = con.prepareStatement("select * from img ");
rs = pst.executeQuery();
while (rs.next()) {
//response.reset();
InputStream in2 = rs.getBinaryStream("name");
in2.read(bu);
response.setContentType("image/jpeg");
in2.close();
}
response.getOutputStream().write(bu);
con.close();
%>
</body>
<title>error.jsp</title>
<%
String path=request.getContextPath();
%>
<body>
<script language="javascript">
alert("上传文件太大!");
document.location.replace("<%=path%>/upload.jsp");
</script>
</body>
<title>uploadsuc.jsp</title>
<%
String path=request.getContextPath();
%>
<body>
<script type="text/javascript">
alert("存入数据库成功!");
document.location.href("<%=path%>/img.jsp");
</script>
</body>
<struts-config>
<data-sources />
<form-beans >
<form-bean name="upldForm" type="mstruts.form.UpldForm" />
</form-beans>
<global-exceptions />
<global-forwards >
<forward name="up" path="/img.jsp"></forward>
<forward name="error" path="/error.jsp"></forward>
<forward name="uploadsuc" path="/uploadsuc.jsp"></forward>
</global-forwards>
<action-mappings >
<action
input="/upload.jsp"
name="upldForm"
path="/upld"
scope="request"
type="mstruts.action.UpldAction" >
</action>
</action-mappings>
<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>
ApplicationResources.properties
fileerrors=<li>/u8bf7/u6d4f/u89c8/u4e0a/u4f20/u6587/u4ef6/uff01
UpldForm.java
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
ActionErrors errors=new ActionErrors();
if(request.getParameter("file")=="")
{
errors.add("file",new ActionMessage("fileerrors"));
}
return errors;
}
UpldAction.java
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UpldForm upldForm = (UpldForm) form;// TODO Auto-generated method stub
try {
String path = new String(request.getParameter("file").getBytes("iso-8859-1"),"gb2312");
String driverName="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/user";
String username="root";
String password="123456";
Connection con;
PreparedStatement pst=null;
try {
Class.forName(driverName);
con=DriverManager.getConnection(url,username,password);
FileInputStream in=new FileInputStream(path);
int m=in.available();//字节数量
if(m>100*1024)
{
return mapping.findForward("error");
}
else
{
HttpSession se=request.getSession();
se.setAttribute("m", m);
pst=con.prepareStatement("insert into img(name) value(?)") ;
pst.setBinaryStream(1,in,in.available());
pst.executeUpdate();
in.close();
con.close();
return mapping.findForward("uploadsuc");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}
<title>upload.jsp</title>
<%
String path = request.getContextPath();
%>
<body>
<html:errors/>
<form action="upld.do" method="post">
<img id="image" name="image" src="<%=path %>/W.JPG" width="100"/>
<input id="file" type="file" name="file" οnchange="setImage()"/>
<input type="submit" value="上传">
</form>
<script language="javascript">
function setImage()
{
document.getElementById("image").src = document.getElementById("file").value;
}
</script>
</body>
<title>img.jsp</title>
<%
HttpSession se = request.getSession();
Integer m = Integer.parseInt(se.getAttribute("m").toString());
%>
<body>
<%
String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/user";
String username = "root";
String password = "123456";
Connection con;
PreparedStatement pst = null;
ResultSet rs;
byte bu[] = new byte[m];
Class.forName(driverName);
con = DriverManager.getConnection(url, username, password);
pst = con.prepareStatement("select * from img ");
rs = pst.executeQuery();
while (rs.next()) {
//response.reset();
InputStream in2 = rs.getBinaryStream("name");
in2.read(bu);
response.setContentType("image/jpeg");
in2.close();
}
response.getOutputStream().write(bu);
con.close();
%>
</body>
<title>error.jsp</title>
<%
String path=request.getContextPath();
%>
<body>
<script language="javascript">
alert("上传文件太大!");
document.location.replace("<%=path%>/upload.jsp");
</script>
</body>
<title>uploadsuc.jsp</title>
<%
String path=request.getContextPath();
%>
<body>
<script type="text/javascript">
alert("存入数据库成功!");
document.location.href("<%=path%>/img.jsp");
</script>
</body>
<struts-config>
<data-sources />
<form-beans >
<form-bean name="upldForm" type="mstruts.form.UpldForm" />
</form-beans>
<global-exceptions />
<global-forwards >
<forward name="up" path="/img.jsp"></forward>
<forward name="error" path="/error.jsp"></forward>
<forward name="uploadsuc" path="/uploadsuc.jsp"></forward>
</global-forwards>
<action-mappings >
<action
input="/upload.jsp"
name="upldForm"
path="/upld"
scope="request"
type="mstruts.action.UpldAction" >
</action>
</action-mappings>
<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>
ApplicationResources.properties
fileerrors=<li>/u8bf7/u6d4f/u89c8/u4e0a/u4f20/u6587/u4ef6/uff01
UpldForm.java
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
ActionErrors errors=new ActionErrors();
if(request.getParameter("file")=="")
{
errors.add("file",new ActionMessage("fileerrors"));
}
return errors;
}
UpldAction.java
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UpldForm upldForm = (UpldForm) form;// TODO Auto-generated method stub
try {
String path = new String(request.getParameter("file").getBytes("iso-8859-1"),"gb2312");
String driverName="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/user";
String username="root";
String password="123456";
Connection con;
PreparedStatement pst=null;
try {
Class.forName(driverName);
con=DriverManager.getConnection(url,username,password);
FileInputStream in=new FileInputStream(path);
int m=in.available();//字节数量
if(m>100*1024)
{
return mapping.findForward("error");
}
else
{
HttpSession se=request.getSession();
se.setAttribute("m", m);
pst=con.prepareStatement("insert into img(name) value(?)") ;
pst.setBinaryStream(1,in,in.available());
pst.executeUpdate();
in.close();
con.close();
return mapping.findForward("uploadsuc");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}