ssh2上传图片到系统文件夹并把图片绝对路径保存到到数据库 ssh2 保存上传的图片路径到数据库ssh数据库 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 两个部分吧,一个是写,一个是读。写的话,图片存到文件系统,数据库里保存一个存放的地址。读的话,把地址取出来,更新img控件的值。楼主是哪里有问题? 你把上传的文件存放在webRoot下的某个特定文件夹,然后将信息保存到数据库,当要查看的时候再从数据库将文件路径找出来,放到src里,<img src="文件路径">。 这是上传文件package com.base.util;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.text.SimpleDateFormat;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;public class FileHelp { //获得存放图片项目路径 public static String getPath(){ HttpServletRequest request = ServletActionContext.getRequest(); String realPath = request.getSession().getServletContext().getRealPath(""); return realPath; } /* * 获取一个记录编号 * 格式:12 位时间+业务编号+随即数 * * 例如:100330101028+04+893 (2010-03-30-10:20:28, 业务号是04, 随机数893 ) * *@param btype 业务类型编号 员工业务01 招聘业务02…… * *@return code 构造的一个记录编号 */ public static String getNo(){ //返回的code String code; //系统当前时间 12位 SimpleDateFormat sdf=new SimpleDateFormat("yyMMddhhmmss"); String nowDate=sdf.format(new java.util.Date()); //随机数 String iRandom = Math.round(Math.random()*900 )+100 +""; //整合一个code return nowDate + iRandom ; } //上传 public static String upload(File file,String name) throws Exception{ // 获得上传图片的后缀名 String aa = name.substring(name.lastIndexOf(".")); // 在给定的路径下存放图片信息 String no = FileHelp.getNo(); File parentDir = new File(FileHelp.getPath()+"\\upload", no + aa); String pname = "upload\\"+ no + aa; // 读下给定路径的文件 File src = new File(FileHelp.getPath()); // 判断文件是否存在,不存在进行创建 if (!src.exists()) { src.mkdirs(); } // 创建输入流 InputStream in = null; // 创建输出流 OutputStream out = null; try { // 上传文件写入请求流中 in = new FileInputStream(file); // 读取流中的数据 byte[] b = new byte[in.available()]; in.read(b); // 把本地读到的文件放到输出流里 out = new FileOutputStream(parentDir); // 写出输出流 out.write(b); } catch (Exception e) { e.printStackTrace(); throw e; } finally { // 及时关闭输入和输出流 if (in != null) in.close(); if (out != null) out.close(); } return pname; } } 这是 daoimpl @Override public void add(Integer id, String picturePath) { Ad ad= new Ad(); ad.setId(id); ad.setPicture(picturePath); this.getHibernateTemplate().save(ad); } 这是serviceimpl@Override public void add(Integer id, String picturePath) { adDao.add(id, picturePath); } jsp中 <s:file name="upload" id="upload" style="width:260px; height:23px;"/>Action中WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(context); String targetDirectory = context.getRealPath("/upload/detail"); String targetFileName = uploadFileName; if (targetFileName != null) { File target = new File(targetDirectory, targetFileName); FileUtils.copyFile(upload, target); setUploadFileName(target.getPath()); 对象 对象名 = new 对象(); 对象名.setlocal("/upload/detail/"+targetFileName); 保存对象入库; 取的时候直接取 <img src="${对象.local}"/> 就可以了快给我分吧 总页数: 总记录数a,每页的大小记为b 如果a被b整除,页数是a/b 如果a不能被b整除,页数是a/b+1 a%b==0?a/b:a/b+1; (a-1)/b+1 (1)a=b*n (b*n-1)/b+1=n-1+1=n (2)a=b*n+m 0<m<n -1<m-1<n-1 (b*n+m-1)/b+1=n+1 who 懂? [email protected] 谁有例子发来吧 想结贴 struts 路径报错 帮忙看下 是求x/y的小数点后300位数字,要求递归实现 eclipse的新手问题 urlrewrite 配置问题 关于辞职问题??? java如何将查出的数据导为excel表 properties中文问题 如何从xml里读取参数 请各位介绍一下讲 java开发实例的好书!!! 谢谢 如何从jar文件中读取资源文件??? 过年闲得无聊、就弄了个网站,大伙给点意见 Map的奇怪问题
写的话,图片存到文件系统,数据库里保存一个存放的地址。
读的话,把地址取出来,更新img控件的值。楼主是哪里有问题?
这是上传文件package com.base.util;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;public class FileHelp {
//获得存放图片项目路径
public static String getPath(){
HttpServletRequest request = ServletActionContext.getRequest();
String realPath = request.getSession().getServletContext().getRealPath("");
return realPath;
}
/* * 获取一个记录编号 * 格式:12 位时间+业务编号+随即数 *
* 例如:100330101028+04+893 (2010-03-30-10:20:28, 业务号是04, 随机数893 )
* *@param btype 业务类型编号 员工业务01 招聘业务02……
* *@return code 构造的一个记录编号
*/
public static String getNo(){
//返回的code String code;
//系统当前时间 12位
SimpleDateFormat sdf=new SimpleDateFormat("yyMMddhhmmss");
String nowDate=sdf.format(new java.util.Date());
//随机数
String iRandom = Math.round(Math.random()*900 )+100 +"";
//整合一个code
return nowDate + iRandom ;
}
//上传
public static String upload(File file,String name) throws Exception{
// 获得上传图片的后缀名
String aa = name.substring(name.lastIndexOf("."));
// 在给定的路径下存放图片信息
String no = FileHelp.getNo();
File parentDir = new File(FileHelp.getPath()+"\\upload", no + aa);
String pname = "upload\\"+ no + aa;
// 读下给定路径的文件
File src = new File(FileHelp.getPath());
// 判断文件是否存在,不存在进行创建
if (!src.exists()) {
src.mkdirs();
}
// 创建输入流
InputStream in = null;
// 创建输出流
OutputStream out = null;
try {
// 上传文件写入请求流中
in = new FileInputStream(file);
// 读取流中的数据
byte[] b = new byte[in.available()];
in.read(b);
// 把本地读到的文件放到输出流里
out = new FileOutputStream(parentDir);
// 写出输出流
out.write(b);
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
// 及时关闭输入和输出流
if (in != null)
in.close();
if (out != null)
out.close();
}
return pname;
}
}
public void add(Integer id, String picturePath) {
Ad ad= new Ad();
ad.setId(id);
ad.setPicture(picturePath);
this.getHibernateTemplate().save(ad);
}
这是serviceimpl@Override
public void add(Integer id, String picturePath) {
adDao.add(id, picturePath);
}
String targetDirectory = context.getRealPath("/upload/detail");
String targetFileName = uploadFileName;
if (targetFileName != null) {
File target = new File(targetDirectory, targetFileName);
FileUtils.copyFile(upload, target);
setUploadFileName(target.getPath());
对象 对象名 = new 对象();
对象名.setlocal("/upload/detail/"+targetFileName);
保存对象入库;
如果a不能被b整除,页数是a/b+1
a%b==0?a/b:a/b+1;
(a-1)/b+1
(1)a=b*n (b*n-1)/b+1=n-1+1=n
(2)a=b*n+m 0<m<n -1<m-1<n-1 (b*n+m-1)/b+1=n+1
who 懂?