能不能个我一段 SSH 上传图片的代码!只将图片的路径保存到数据库中!
2个回答
展开全部
给你一段!希望能帮到你。
/**
* @see跳转方法
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws IOException
*/
public ActionForward toadd(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws IOException
{
HoteForm hoteForm = (HoteForm) form;
saveToken(request);//生成一个TOKEN
return mapping.findForward("add");
}
/**
* @see执行添加方法
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws IOException
*/
public ActionForward doadd(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws IOException
{
HoteForm hoteForm = (HoteForm) form;
if(this.isTokenValid(request,true))
{
//上传文件
String dir = this.getServlet().getServletContext().getRealPath("/upLoadFiles/hoteimg");
if(form instanceof HoteForm)
{
FormFile file = hoteForm.getTheFile();
//定义图片格式
List<String> list = new ArrayList();
list.add("jpg");
list.add("jpeg");
list.add("gif");
list.add("bmp");
if(!dir.endsWith("/"))
{
dir = dir.concat("/");
if(file != null)
{
String fileName = file.getFileName();//获取文件名
int fsize = file.getFileSize();//获取文件大小
String ext = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());// 获取文件类型,即扩展名
fileName = getdateRenameFileName()+"."+ext;//文件重命名
ext = ext.toLowerCase();//转换为小写
//判断是否是要求的上传的类型图片
if(!list.contains(ext))
{
response.getWriter().print("<script>alert('不支持该文件!');location.href='hote.do?operate=toadd'</script>");
return null;
}
if(fsize > 1024*1024)
{
response.getWriter().print("<script>alert('文件过大!');location.href='hote.do?operate=toadd'</script>");
return null;
}
try {
InputStream stream = file.getInputStream();
OutputStream bos = new FileOutputStream(dir+"/"+fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while((bytesRead = stream.read(buffer,0,8192)) != -1)
{
bos.write(buffer,0,bytesRead);
}
bos.close();
} catch (Exception e) {
// TODO: handle exception
}
file.destroy();
hoteForm.getHote().setImage(fileName);
}
}
}
// 获取系统当前时间
java.util.Date date = Calendar.getInstance().getTime();
// 时间格式化 年-月-日
String time = new SimpleDateFormat("yyyy-MM-dd").format(date);
hoteForm.getHote().setTime(time);
Users user = (Users)request.getSession().getAttribute("user");
hoteForm.getHote().setUsers(user);
if(hoteForm.getHote() != null)
{
if(this.hoteBiz.add(hoteForm.getHote()))
{
response.getWriter().print("<script> alert('添加成功!');location.href='hote.do?operate=getHoteList'</script>");
}
}
response.getWriter().print("<script> alert('添加失败!');javascript:history.go(-1);</script>");
}else
{
response.getWriter().print("<script> alert('不能重复提交数据!');javascript:history.go(-1);</script>");
}
return null;
}
/**
* @see图片重命名
* @return
*/
public String getdateRenameFileName() {
java.text.SimpleDateFormat formatter_f = new java.text.SimpleDateFormat(
"yyyyMMddHHmmss");
java.util.Date currentTime_f = new java.util.Date(); // 得到当前系统时间
String new_date_f = formatter_f.format(currentTime_f); // 将日期时间格式化
return new_date_f;
}
/**
* @see跳转方法
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws IOException
*/
public ActionForward toadd(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws IOException
{
HoteForm hoteForm = (HoteForm) form;
saveToken(request);//生成一个TOKEN
return mapping.findForward("add");
}
/**
* @see执行添加方法
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws IOException
*/
public ActionForward doadd(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws IOException
{
HoteForm hoteForm = (HoteForm) form;
if(this.isTokenValid(request,true))
{
//上传文件
String dir = this.getServlet().getServletContext().getRealPath("/upLoadFiles/hoteimg");
if(form instanceof HoteForm)
{
FormFile file = hoteForm.getTheFile();
//定义图片格式
List<String> list = new ArrayList();
list.add("jpg");
list.add("jpeg");
list.add("gif");
list.add("bmp");
if(!dir.endsWith("/"))
{
dir = dir.concat("/");
if(file != null)
{
String fileName = file.getFileName();//获取文件名
int fsize = file.getFileSize();//获取文件大小
String ext = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());// 获取文件类型,即扩展名
fileName = getdateRenameFileName()+"."+ext;//文件重命名
ext = ext.toLowerCase();//转换为小写
//判断是否是要求的上传的类型图片
if(!list.contains(ext))
{
response.getWriter().print("<script>alert('不支持该文件!');location.href='hote.do?operate=toadd'</script>");
return null;
}
if(fsize > 1024*1024)
{
response.getWriter().print("<script>alert('文件过大!');location.href='hote.do?operate=toadd'</script>");
return null;
}
try {
InputStream stream = file.getInputStream();
OutputStream bos = new FileOutputStream(dir+"/"+fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while((bytesRead = stream.read(buffer,0,8192)) != -1)
{
bos.write(buffer,0,bytesRead);
}
bos.close();
} catch (Exception e) {
// TODO: handle exception
}
file.destroy();
hoteForm.getHote().setImage(fileName);
}
}
}
// 获取系统当前时间
java.util.Date date = Calendar.getInstance().getTime();
// 时间格式化 年-月-日
String time = new SimpleDateFormat("yyyy-MM-dd").format(date);
hoteForm.getHote().setTime(time);
Users user = (Users)request.getSession().getAttribute("user");
hoteForm.getHote().setUsers(user);
if(hoteForm.getHote() != null)
{
if(this.hoteBiz.add(hoteForm.getHote()))
{
response.getWriter().print("<script> alert('添加成功!');location.href='hote.do?operate=getHoteList'</script>");
}
}
response.getWriter().print("<script> alert('添加失败!');javascript:history.go(-1);</script>");
}else
{
response.getWriter().print("<script> alert('不能重复提交数据!');javascript:history.go(-1);</script>");
}
return null;
}
/**
* @see图片重命名
* @return
*/
public String getdateRenameFileName() {
java.text.SimpleDateFormat formatter_f = new java.text.SimpleDateFormat(
"yyyyMMddHHmmss");
java.util.Date currentTime_f = new java.util.Date(); // 得到当前系统时间
String new_date_f = formatter_f.format(currentTime_f); // 将日期时间格式化
return new_date_f;
}
镭速传输
2024-10-28 广告
2024-10-28 广告
在深圳市云语科技有限公司,我们深知远程传输大文件的重要性与便捷性。为此,我们推荐使用高效的文件传输服务,这些服务通常支持断点续传,确保大文件传输的稳定与安全。用户只需简单上传,系统即可自动处理,无论文件大小,都能实现快速、可靠的远程传输。同...
点击进入详情页
本回答由镭速传输提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询