小弟 请教个问题
您好 我想问一下 上传图片到服务器 把图片地址 信息保存到数据库 点击刷新按钮怎么在把图片信息从数据库拿出来显示在后台的 grid中 谢谢 不胜感谢 --------------------编程问答-------------------- 你这边的刷新操作就相当于一次查询全部信息的操作,点击刷新按钮的时候,通过单击事件,在js中写一个连接后台的方法,在后台查询出所有的图片信息,生成一个list返回页面,然后再在页面上循环遍历出来 --------------------编程问答-------------------- 你可以用ajax异步请求刷新图片,就写在刷新按钮的方法里面。 --------------------编程问答-------------------- 写了 但是最后一步 数据库返回的值 怎么跟grid联系一块 老是出错 --------------------编程问答-------------------- 把表单中上传的图片保存在服务器 把图片路径和填写的信息保存在数据库 同时把图片信息传到后面页面的grid中 是现在表单信息传到grid 还是传到数据库 在从数据库拿数据到grid 这个问题纠结两天了谢谢 --------------------编程问答-------------------- 你可以看一下有关验证码的相关东西
就是你要的那种
--------------------编程问答-------------------- 可以试试这个方法!
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.iprecare.entity.management.product.Product;
import com.iprecare.service.ProductsService;
import com.iprecare.serviceImpl.ProductsServiceImpl;
import com.iprecare.util.Commons;
import com.iprecare.util.GetNowTime;
public class AddProductServlet extends HttpServlet {
private String savePath="/images/product";
ServletContext sc;
private Map<String,String> parameters=new HashMap<String, String>();
private static final long serialVersionUID = 1L;
public void init(ServletConfig config)
{
sc=config.getServletContext();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
File file = doAttachment(request);
String pic ="nopic.gif";
String poto=parameters.get("pic");
if(file ==null&&poto!=null){
pic =poto;
}
if(file!=null){
pic=file.toString();
}
PrintWriter out = response.getWriter();
String productName=parameters.get("productName");
float productMatkPrice=Float.parseFloat(parameters.get("productMatkPrice"));
float productMembPrice=Float.parseFloat(parameters.get("productMembPrice"));
String productType=parameters.get("productType");
String productDesc=parameters.get("productDesc");
//String productName=request.getParameter("productName");
//String aa=request.getParameter("productMatkPrice");
//float productMatkPrice=Float.parseFloat(aa);
//String bb=request.getParameter("productMembPrice");
//float productMembPrice=Float.parseFloat(bb);
//String productType=request.getParameter("productType");
//String productImg=request.getParameter("productImg");
//String productDesc=request.getParameter("productDesc");
//System.out.println("productImg="+productImg);
Product pd=new Product();
pd.setProductName(productName);
pd.setProductMatkPrice(productMatkPrice);
pd.setProductMembPrice(productMembPrice);
pd.setProductType(productType);
pd.setProductImg(pic);
pd.setProductDesc(productDesc);
ProductsService pds=new ProductsServiceImpl();
pds.AddProducts(pd);
//request.setAttribute("error", "<script language = javascript>alert('信息保存成功!');location.href='/iPreCare/management/products/addProducts.jsp'</script>");
//request.getRequestDispatcher("index.jsp").forward(request, response);
//return;
out.print(Commons.getJS("添加成功!", "/iPreCare/management/products/addProducts.jsp"));
}
public File doAttachment(HttpServletRequest request) throws ServletException, IOException {
File file = null;
String filename=null;
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if (item.isFormField()) {
parameters.put(item.getFieldName(), item.getString("UTF-8"));
} else {
if (item.getName() != null && !item.getName().equals("")) {
File tempFile = new File(item.getName());
filename = tempFile.getName();
// String suffix=filename.substring(filename.lastIndexOf('.'));
//filename=GetNowTime.getNewTime()+filename;
System.out.println(filename +" "+filename);
file = new File(sc.getRealPath("/") + savePath, filename);
System.out.println("d="+sc.getRealPath("/"));
item.write(file);
file = new File(filename);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
} --------------------编程问答-------------------- 大哥 你写的doPost()这个方法是干嘛的 图片上传到服务器 地址跟信息 放到数据库也做好了 我想问一下 怎么把信息同时在放入grid 中 --------------------编程问答-------------------- 你写的doPost()这个方法是干嘛的 图片上传到服务器 地址跟信息 放到数据库也做好了 我想问一下 怎么把信息同时在放入grid 中 --------------------编程问答-------------------- 会用Ajax不,?用ajax试试 --------------------编程问答--------------------
补充:Java , Web 开发