答案:<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!-- basePath 代表你项目的绝对路径 -->
先确保jsp 含有以上代码 ,在img scr 属性前面添加 <%=basePath %> jsp代码就可以确保 是绝对路径 以此存入数据库 就永远不会出错 。项目里建议实用绝对路径。以这种方法可以轻松解决相对路径与 绝对路径 的问题 。
读取就很简单了 ,img 的src 属性 直接el表达式 ${ 变量 } .比如 <img src="${ 路径 }" />
如果还不懂,qq:757690299
其他:页面上用img标签,然后从数据库读取所需图片路径,将该值赋给img标签的src属性就OK 到了一个apache的一个组件,fileupload,你自己在网上下载吧,很方便用的。
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import oracle.sql.BLOB;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* @author Lenovo-User
*
*/
public class UploadFileAction extends Action {
/* (non-Javadoc)
* @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@SuppressWarnings("deprecation")
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
UploadFileForm fForm = (UploadFileForm)form;
DataSource ds = this.getDataSource(request);
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
//PersonBean pb = (PersonBean)request.getSession().getAttribute(Constants.PERSON_KEY);
String strId = request.getParameter("id");
int id = 0;
if(strId != null)
id = Integer.parseInt(strId);
ResultSet rs = stmt.executeQuery("select * from user_account where id=" + id + " for update");
rs.next();
oracle.sql.BLOB blob = (BLOB) rs.getBlob(4);
OutputStream out = blob.getBinaryOutputStream();
int bufferSize = blob.getBufferSize();
InputStream in = fForm.getMyFile().getInputStream();
byte bytes[] = new byte[bufferSize];
while(in.read(bytes) != -1 ) {
out.write(bytes, 0, bufferSize);
}
in.close();
out.flush();//后来添加的
out.close();
rs.close();
stmt.close();
conn.commit();//后来添加的!必须要提交,不管是不是数据库是不是自动 提交
conn.close();
return mapping.findForward("success");
}
}
不懂的可以问我
上一个:用jsp做票务系统的话该下载哪些软件?代码往哪添?
下一个:求一个基于jsp的网上商城源代码,用mysql tomcat eclipse 做的 感激万分啊