【Java开源代码栏目提醒】:网学会员为广大网友收集整理了,AddProductServlet.java,希望对大家有所帮助!
锘縫ackage adminshop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public final class AddProductServlet extends HttpServlet{
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Integer id = (Integer)req.getAttribute("id");
Integer sortid = (Integer)req.getAttribute("sortid");
String name = (String)req.getAttribute("name");
String price = (String)req.getAttribute("price");
String saleprice = (String)req.getAttribute("saleprice");
String descript = (String)req.getAttribute("descript");
String contents = (String)req.getAttribute("contents");
File theFile = (File)req.getAttribute("theFile");
/*
* 瀹屾垚涓庢暟鎹簱鐩稿叧鐨勬搷浣?鎻掑叆鍟嗗搧璁板綍
*/
DB db=new DB();
shopadmin sa=new shopadmin();
Product product=null;
Sort sort=null;
if (id.intValue()==0) { //鏂版彃鍏ュ晢鍝?
product = new Product();
}
else{ //淇敼鍟嗗搧
try {
product = sa.getProduct(db, id.intValue());
} catch (Exception e) {
// TODO 鑷姩鐢熸垚 catch 鍧?
e.printStackTrace();
}
}
try {
sort = sa.getSort(db, sortid.intValue());
} catch (Exception e1) {
// TODO 鑷姩鐢熸垚 catch 鍧?
e1.printStackTrace();
}
//product.setId(sort);
product.setName(name);
product.setPrice(Double.parseDouble(price));
product.setSaleprice(Double.parseDouble(saleprice));
product.setDescript(descript);
product.setContents(contents);
product.setSalecount(0);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
product.setSaledate( df.format(new Date()));
/*
* 灏嗕笂浼犳枃浠朵繚瀛樺埌鎸囧畾鏂囦欢澶?
*/
if (!theFile.getName().equals("")){
try {
InputStream stream = ((ServletRequest) theFile).getInputStream(); //鎶婃枃浠惰鍏?
String filePath = req.getRealPath("/"); //取锟斤拷前
系统鍙栧綋鍓嶇郴缁熻矾寰?
filePath = filePath.substring(0,filePath.length()-1);
int i = filePath.lastIndexOf("\\");
System.out.println(i);
System.out.println(filePath);
filePath = filePath.substring(0,i);
filePath = filePath + "\\images\\";
OutputStream bos = new FileOutputStream( filePath + theFile.getName()); //寤虹珛涓涓笂浼犳枃浠剁殑杈撳嚭娴?
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead); //灏嗘枃浠跺啓鍏ユ湇鍔″櫒
}
bos.close();
stream.close();
} catch (Exception e) {
System.err.print(e);
}
product.setImage("images/"+ theFile.getName());
}
try {
sa.saveOrUpdate(product);
} catch (Exception e) {
// TODO 鑷姩鐢熸垚 catch 鍧?
e.printStackTrace();
}//淇濆瓨鍟嗗搧瀵硅薄
/*
*杞嚦淇濆瓨鎴愬姛鎻愮ず椤甸潰
*/
//ActionMessages errors = new ActionMessages();
ArrayList error = null;
error.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessages("淇濆瓨鎴愬姛锛侊紒"));
if (!error.isEmpty()) {
saveErrors(req, error);
}
resp.sendRedirect("seccss.jsp"); //淇濆瓨鎴愬姛鎻愮ず椤甸潰
}
private void saveErrors(HttpServletRequest req, ArrayList error) {
// TODO 鑷姩鐢熸垚鏂规硶瀛樻牴
}
}