急~~~WEB网站后台管理,关于实现上传功能的问题,跪求高手解答```

在做管理【新闻】板块时,管理员更新新闻页面的内容,现在的想法是能够把一个包含文字和图片的页面通过后台管理直接上传,然后可以在【新闻】页面自动更新单纯的上传图片已经研究透了... 在做管理【新闻】板块时,管理员更新新闻页面的内容,现在的想法是能够把一个包含文字和图片的页面通过后台管理直接上传,然后可以在【新闻】页面自动更新 单纯的上传图片已经研究透了,但是一起上传文字和图片,而且要实现起来方便,只要简单的排版就可以将效果在前台显示 忘高手出手相救,只剩两天时间``` 具体:1.数据库要怎么设计?2.图片要怎么储存在表中?3.要用什么特殊的java包?4.我的是Jsp页面的,所以asp的可以飘过。5.我倾家荡产的给分(虽然我很少分...) 展开
 我来答
匿名用户
2013-05-18
展开全部
这是数据库连接的一段代码import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import com.yxq.toolsbean.DB;
import com.yxq.valuebean.PhotoBean;public class PhotoDao {
private DB connection = null;
public PhotoDao() {
connection = new DB();
} public boolean operationPhoto(String operation, PhotoBean single) {
String sql = "";
if (operation.equals("delete"))
sql = "delete from tb_photo where id=" + single.getId();
if (operation.equals("upload"))
sql = "insert into tb_photo values (null,'"+ single.getPhotoAddr() + "','"+ single.getPhotoTime() + "','"+ single.getPhotoInfo() + "')";
boolean mark=connection.executeUpdate(sql);
return mark;
} public PhotoBean queryPhoto(int id) {
PhotoBean photoBean = null;
String sql = "select * from tb_photo where id=" + id;
ResultSet rs = connection.executeQuery(sql);
try {
if(rs.next()) {
photoBean = new PhotoBean();
photoBean.setId(rs.getInt(1));
photoBean.setPhotoAddr(rs.getString(2));
photoBean.setPhotoInfo(rs.getString(3));
photoBean.setPhotoTime(rs.getString(4));
}
} catch (SQLException e) {
e.printStackTrace();
}
return photoBean;
} public List queryPhoto(String type) {
if(type==null||type.equals(""))
type="sub";
String sql = "";
if(type.equals("all"))
sql="select * from tb_photo order by photo_sdTime DESC";
else
sql="select * from tb_photo order by photo_sdTime DESC limit 8";

List list = null;
PhotoBean photoBean = null;
ResultSet rs = connection.executeQuery(sql);
if(rs!=null){
list=new ArrayList();
try {
while (rs.next()) {
photoBean = new PhotoBean();
photoBean.setId(rs.getInt(1));
photoBean.setPhotoAddr(rs.getString(2));
photoBean.setPhotoTime(rs.getString(3));
photoBean.setPhotoInfo(rs.getString(4));
list.add(photoBean);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
} public int queryMaxId() {
int maxId = 0;
String sql = "select max(id) from tb_photo";
ResultSet rs = connection.executeQuery(sql);
if(rs!=null){
try {
if(rs.next())
maxId = rs.getInt(1);
} catch (SQLException e) {
e.printStackTrace();
}finally{
connection.closed();
}
}
return maxId;
}
}这是上传的代码,用前要导入一个jspSmartUpload包import java.io.IOException;
import java.util.Date;
import java.util.List;import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.yxq.dao.PhotoDao;
import com.yxq.toolsbean.MyTools;
import com.yxq.valuebean.PhotoBean;public class PhotoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response); }

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if(action==null)
action="";
if (action.equals("upload"))
this.addPhoto(request, response); //上传图片
if (action.equals("delete"))
this.deletePhoto(request, response); //删除图片
if(action.equals("list"))
this.selectPhoto(request,response); //查询所有图片
if(action.equals("single"))
this.singlePhoto(request,response); //查看图片详细内容
if(action.equals("adminList"))
this.adminSelectPhoto(request,response); //查询所有图片
}
/**
* @功能 前台-查询所有图片
*/
public void selectPhoto(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PhotoDao photoDao=new PhotoDao();
List photoList=photoDao.queryPhoto("all");
request.setAttribute("photoList", photoList);

RequestDispatcher rd=request.getRequestDispatcher("/front/photo/PhotoList.jsp");
rd.forward(request,response);
}
/**
* @功能 后台-查询所有图片
*/
public void adminSelectPhoto(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PhotoDao photoDao=new PhotoDao();
List photoList=photoDao.queryPhoto("all");
request.setAttribute("photoList", photoList);

RequestDispatcher rd=request.getRequestDispatcher("/admin/photo/PhotoList.jsp");
rd.forward(request,response);
}
/**
* @功能 查看某个图片详细内容
*/
public void singlePhoto(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PhotoDao photoDao=new PhotoDao();
String strId=request.getParameter("id");
int id=MyTools.strToint(strId);
PhotoBean photoSingle=photoDao.queryPhoto(id);
request.setAttribute("photoSingle",photoSingle);

RequestDispatcher rd=request.getRequestDispatcher("/front/photo/PhotoSingle.jsp");
rd.forward(request,response);
} /**
* @功能 删除图片
*/
public void deletePhoto(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String messages="";
String forward="";
String href="";

RequestDispatcher rd=null;
PhotoDao photoDao= new PhotoDao();
int id = MyTools.strToint(request.getParameter("id"));
String fileAddr=photoDao.queryPhoto(id).getPhotoAddr();
String photoDir=request.getSession().getServletContext().getRealPath("\\");
String delFile=photoDir+fileAddr;

java.io.File file = new java.io.File(delFile);

PhotoBean photoBean = new PhotoBean();
photoBean.setId(id);
if (photoDao.operationPhoto("delete", photoBean)) {
boolean result=file.delete();
if(result){
messages="<li>删除照片成功!</li>";
forward="/admin/success.jsp";
href="<a href='PhotoServlet?action=adminList'>[继续删除其他照片]</a>";
}
else{
messages="<li>删除照片失败!</li>";
forward="/admin/error.jsp";
href="<a href='javascript:window.history.go(-1)'>[返回]</a>";
}

} else {
messages="<li>删除照片失败!</li>";
forward="/admin/error.jsp";
href="<a href='javascript:window.history.go(-1)'>[返回]</a>";
}
request.setAttribute("messages",messages);
request.setAttribute("href",href);
rd=request.getRequestDispatcher(forward);
rd.forward(request,response);

}
/**
* @功能 上传图片
*/
public void addPhoto(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
String filePath = "/front/photo/pic/";
String messages="";
String forward="";
String href="";

PhotoDao photoDao = new PhotoDao();
PhotoBean photoBean = new PhotoBean();
SmartUpload su = new SmartUpload();
long maxsize = 2 * 1024 * 1024; // 设置每个上传文件的大小,为2MB

try {
su.initialize(this.getServletConfig(), request, response);
su.setMaxFileSize(maxsize); // 限制上传文件的大小
su.setAllowedFilesList("jpg,gif,bmp"); // 设置允许上传的文件类型
su.upload(); // 上传文件

String photoInfo=su.getRequest().getParameter("info");
if(photoInfo==null||photoInfo.equals("")){ //验证照片描述信息,若没有输入,则提示输入照片描述信息
messages="请输入照片描述信息!";
forward="/admin/error.jsp";
href="<a href='javascript:window.history.go(-1)'>[返回]</a>";
}
else{
File file = su.getFiles().getFile(0); // 获取上传的文件,因为只上传了一个文件,所以可直接获取
if (!file.isMissing()) { // 如果选择了文件
String photoAddr=filePath+photoDao.queryMaxId()+"."+file.getFileExt(); //filePath值(front\\photo\\pic\\)+图片信息在数据表中的id字段值+“.”+文件后缀名;最后生成例如“front\photo\pic\12.bmp”路径
String now=MyTools.changeTime(new Date()); //获取当前时间并格式化为字符串

photoBean.setPhotoAddr(photoAddr);
photoBean.setPhotoTime(now);
photoBean.setPhotoInfo(photoInfo);

boolean mark=photoDao.operationPhoto("upload",photoBean);
if(mark){
try {
file.saveAs(photoAddr,File.SAVEAS_VIRTUAL);
messages="上传文件成功!";
forward="/admin/success.jsp";
href="<a href='admin/photo/PhotoUpload.jsp'>[继续上传]</a>";
} catch (SmartUploadException ee) {
messages="上传文件失败!";
forward="/admin/error.jsp";
href="<a href='javascript:window.history.go(-1)'>[返回]</a>";
ee.printStackTrace();
}
}
else{
messages="保存文件信息失败!";
forward="/admin/error.jsp";
href="<a href='javascript:window.history.go(-1)'>[返回]</a>";
}
}
else{
messages="请选择要上传的文件!";
forward="/admin/error.jsp";
href="<a href='javascript:window.history.go(-1)'>[返回]</a>";
}
}

}catch (java.lang.SecurityException e){
messages="<li>上传文件失败!上传的文件类型只允许为:jpg,gif,bmp</li>";
forward="/admin/error.jsp";
href="<a href='javascript:window.history.go(-1)'>[返回]</a>";

}catch (SmartUploadException e) {
messages="上传文件失败!";
forward="/admin/error.jsp";
href="<a href='javascript:window.history.go(-1)'>[返回]</a>";
e.printStackTrace();
}

request.setAttribute("messages",messages);
request.setAttribute("href",href);

RequestDispatcher requestDispatcher = request.getRequestDispatcher(forward);
requestDispatcher.forward(request, response);
}
}
匿名用户
2013-05-18
展开全部
在线编缉器,建议用
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式