JSP中调用servlet上传图片 10
packagecom.jsp.servlet;importjava.io.File;importjava.io.IOException;importjava.util.L...
package com.jsp.servlet;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.AbstractDocument.Content;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.jsp.model.Upload;
import com.jsp.service.UploadService;
public class UploadServlet extends HttpServlet{
private static String path="";
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
path=this.getServletContext().getRealPath("/work");
try{
DiskFileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload up=new ServletFileUpload(factory);
List<FileItem> ls=up.parseRequest(request);
for(FileItem fileItem:ls){
if(fileItem.isFormField()){
String FieldName=fileItem.getFieldName();
String Content= fileItem.getString("utf-8");
request.setAttribute(FieldName,Content);
String title = request.getParameter("title");
String notes = request.getParameter("textarea");
String state = request.getParameter("state");
Upload upload=new Upload();
upload.setTitle(title);
upload.setImagesname(FieldName);
upload.setNotes(notes);
upload.setState(state);
//调用UploadService完成用户上传作品的业务逻辑
UploadService service = new UploadService();
service.upload(upload);
}else{
String nm=fileItem.getName().substring(fileItem.getName().lastIndexOf("\\")+1);
File mkr=new File(path,nm);
if(mkr.createNewFile()){
fileItem.write(mkr);
}
request.setAttribute("result","上传文件成功!");
}
}
}
catch(Exception e){
e.printStackTrace();
request.setAttribute("result","上传失败,请查找原因,重新再试!");
}
request.getRequestDispatcher("/works_exhibition.jsp").forward(request, response);
}
} 展开
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.AbstractDocument.Content;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.jsp.model.Upload;
import com.jsp.service.UploadService;
public class UploadServlet extends HttpServlet{
private static String path="";
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
path=this.getServletContext().getRealPath("/work");
try{
DiskFileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload up=new ServletFileUpload(factory);
List<FileItem> ls=up.parseRequest(request);
for(FileItem fileItem:ls){
if(fileItem.isFormField()){
String FieldName=fileItem.getFieldName();
String Content= fileItem.getString("utf-8");
request.setAttribute(FieldName,Content);
String title = request.getParameter("title");
String notes = request.getParameter("textarea");
String state = request.getParameter("state");
Upload upload=new Upload();
upload.setTitle(title);
upload.setImagesname(FieldName);
upload.setNotes(notes);
upload.setState(state);
//调用UploadService完成用户上传作品的业务逻辑
UploadService service = new UploadService();
service.upload(upload);
}else{
String nm=fileItem.getName().substring(fileItem.getName().lastIndexOf("\\")+1);
File mkr=new File(path,nm);
if(mkr.createNewFile()){
fileItem.write(mkr);
}
request.setAttribute("result","上传文件成功!");
}
}
}
catch(Exception e){
e.printStackTrace();
request.setAttribute("result","上传失败,请查找原因,重新再试!");
}
request.getRequestDispatcher("/works_exhibition.jsp").forward(request, response);
}
} 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励10(财富值+成长值)+提问者悬赏10(财富值+成长值)
1个回答
2014-10-14
展开全部
哪里报错?报的什么错贴出来,我帮你看看
追问
插入到数据库的数据都是表单里文本框的name属性的值,而且都只是放在一列里
追答
你不要取name属性?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询