Java web 问题图片怎么存储到数据库。 50
展开全部
package shop.car.control;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;
import shop.car.model.GoodService;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
public class ImageInput 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 {
PrintWriter out = response.getWriter();
PageContext pageContext = null;
JspFactory jsp = null;
// 实例化上载bean
SmartUpload mySmartUpload = new SmartUpload();
jsp = JspFactory.getDefaultFactory();
pageContext = jsp.getPageContext(this, request, response, "", true,
8192, true);
// 初始化
mySmartUpload.initialize(pageContext);
// 设置上载的最大值
mySmartUpload.setMaxFileSize(500 * 1024 * 1024);
// 设定允许上传的文件(通过扩展名限制)
mySmartUpload.setAllowedFilesList("jpg");
// 上载文件
try {
mySmartUpload.upload();
} catch (SmartUploadException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
int count = 0;
try {
count = mySmartUpload.save("/upload", SmartUpload.SAVE_VIRTUAL);
} catch (SmartUploadException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out.println(count + "个文件上传成功!<br>");
out.println("TEST=" + mySmartUpload.getRequest().getParameter("TEST")
+ "<BR><BR>");
// 循环取得所有上载的文件
for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {
// 取得上载的文件
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(
i);
if (!myFile.isMissing()) {
// 取得上载的文件的文件名
String myFileName = myFile.getFileName();
// 取得不带后缀的文件名
String suffix = myFileName.substring(0, myFileName
.lastIndexOf('.'));
// 取得后缀名
String ext = mySmartUpload.getFiles().getFile(0).getFileExt();
// 取得文件的大小
int fileSize = myFile.getSize();
// 保存路径
String aa = request.getRealPath(request.getServletPath())
+ myFileName;
String trace = aa + myFileName;
String s=new String(trace.getBytes("ISO-8859-1"),"utf-8");
// 取得别的参数
String explain = (String) mySmartUpload.getRequest()
.getParameter("text");
String send = (String) mySmartUpload.getRequest().getParameter(
"send");
// 将文件保存在服务器端
try {
myFile.saveAs(trace, SmartUpload.SAVE_PHYSICAL);
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 下面的是将上载的文件保存到数据库中
// 将文件读到流中
java.io.File file = new java.io.File(s);
java.io.FileInputStream fis = new java.io.FileInputStream(file);
out.println(file.length());
GoodService.addGoods(fis, file);
out.println(("上载成功!!!").toString());
} else {
out.println(("上载失败!!!").toString());
}
}// 与前面的if对应
out.flush();
out.close();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询