本帖最后由 丁岩 于 2013-5-28 21:53 编辑
JSP界面表单代码- <form action="/Web/TitMsgServlet" method="post" enctype="multipart/form-data">
- <div id="sign"><label id="t1">个性签名:</label> <textarea name= "sign" cols= "25" rows= "2" style="font-size:24px; font-family:'黑体';"></textarea> <span id="e">不能超过50字</span></div>
- <div id="img">
- <span id="t2">头像上传:</span>
- <input type=file name="img" id="doc" onchange= "javascript:setImagePreview();" accept="image/jpeg,image/gif,image/png,image/bmp">
- <p><div id="localImag"><img id="preview" width=-1 height=-1 style="diplay:none" /></div></p>
- </div>
- <input type="hidden" id="hid" name="hid" value=""/>
- <div id="sm">
- <input type="submit" value="保存设置"/>
- </div>
- </form>
复制代码 TitMsgServlet代码- package web.controller;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.MultipartConfig;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.commons.fileupload.FileItemIterator;
- import org.apache.commons.fileupload.FileItemStream;
- import org.apache.commons.fileupload.disk.DiskFileItemFactory;
- import org.apache.commons.fileupload.servlet.ServletFileUpload;
- import org.apache.commons.fileupload.util.Streams;
- @MultipartConfig
- public class TitMsgServlet extends HttpServlet implements javax.servlet.Servlet{
- File tmpDir = new File("C:\\abc");//初始化上传文件的临时存放目录
- File saveDir = new File("C:\\apache-tomcat-7.0.37\\webapps\\Web\\upload");//初始化上传文件后的保存目录
- public TitMsgServlet()
- {
- super();
- }
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException
- {
-
- try{
- if(ServletFileUpload.isMultipartContent(request)){
- DiskFileItemFactory dff = new DiskFileItemFactory();//创建该对象
- dff.setRepository(tmpDir);//指定上传文件的临时目录
- dff.setSizeThreshold(1024000);//指定在内存中缓存数据大小,单位为byte
- ServletFileUpload sfu = new ServletFileUpload(dff);//创建该对象
- sfu.setFileSizeMax(5000000);//指定单个上传文件的最大尺寸
- sfu.setSizeMax(10000000);//指定一次上传多个文件的总尺寸
- FileItemIterator fii = sfu.getItemIterator(request);//解析request 请求,并返回FileItemIterator集合
- while(fii.hasNext()){
- FileItemStream fis = fii.next();//从集合中获得一个文件流
- if(!fis.isFormField() && fis.getName().length()>0){//过滤掉表单中非文件域
- String fileName = "biji.jpg";//获得上传文件的文件名
- BufferedInputStream in = new BufferedInputStream(fis.openStream());//获得文件输入流
- BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(saveDir+fileName)));//获得文件输出流
- Streams.copy(in, out, true);//开始把文件写到你指定的上传文件夹
- }
- }
- response.getWriter().println("File upload successfully!!!");//终于成功了,还不到你的上传文件中看看,你要的东西都到齐了吗
- }
- }catch(Exception e){
- e.printStackTrace();
- }
-
- }
-
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request,response);
- }
-
-
- }
复制代码 疯了。。。。这个显示能成功,但是目标目录还是没有上传的文件,有没有人帮我改改这个代码,或者提出你的解决方案,要求文件能上传到服务器目录就好了
|