BBS项目,头像存在哪个路径下?
   我的做法是:
存到 D:/workspace/bbs/faces/[userid].jpg    (开发时myeclipse的工作空间里)
或D:/programs/apache tomcat/webapp/bbs/faces/[userid].jpg(开发时tomcat的webapp里)
这样做有这么几个问题:
    1、项目开发完之后,存储路径怎么改呢?机器变了,路径肯定不一样了
    2、这个上传完后,不管是什么格式的图片都被强制命名成jpg格式的了,有什么方法可以不改变格式,这个方法是尚学堂bbs项目里的,在网上找了一些,没看懂。高分请高手提供代码!感激不尽!ps:图片根本没有往数据库里存,显示的页面直接<img src="<%=user.getId()%>".jpg/>

解决方案 »

  1.   

    1、项目开发完之后,存储路径怎么改呢?机器变了,路径肯定不一样了要写相对路径2、这个上传完后,不管是什么格式的图片都被强制命名成jpg格式的了,有什么方法可以不改变格式,这个方法是尚学堂bbs项目里的,在网上找了一些看你怎么写方法了 把代码贴出来 
      

  2.   


    package com.xidian._3dcampus.servlet;import java.io.IOException;
    import java.io.PrintWriter;import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    import org.apache.commons.fileupload.*;
    import java.util.*;
    import java.util.regex.*;
    import java.io.*;
    import org.apache.commons.fileupload.servlet.*;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;public class BbsFileUpload extends HttpServlet {  private static final long serialVersionUID = 1L; public void init(ServletConfig config) throws ServletException {
    this.config = config;
    } private ServletConfig config = null;

    private File tempPath = new File("C:\\Documents and Settings\\"); // 用于存放临时文件的目录 public void destroy() {
    config = null;
    super.destroy();
    }
    public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
    int id = -1;


    String uploadPath = config.getInitParameter("uploadPath"); // 用于存放上传文件的目录 res.setContentType("text/html; charset=GB2312");
    PrintWriter out = res.getWriter(); DiskFileItemFactory factory = new DiskFileItemFactory();
    // maximum size that will be stored in memory
    factory.setSizeThreshold(40960);
    // the location for saving data that is larger than getSizeThreshold()
    factory.setRepository(tempPath); ServletFileUpload upload = new ServletFileUpload(factory);
    // maximum size before a FileUploadException will be thrown
    upload.setSizeMax(60000000);
    try {
    List fileItems = upload.parseRequest(req);
    // assume we know there are two files. The first file is a small
    // text file, the second is unknown and is written to a file on
    // the server
    Iterator iter = fileItems.iterator(); // 正则匹配,过滤路径取文件名
    String regExp = ".+\\\\(.+)$"; // 过滤掉的文件类型
    String[] errorType = { ".exe", ".com", ".cgi", ".jsp" };
    Pattern p = Pattern.compile(regExp);
    while (iter.hasNext()) {
    FileItem item = (FileItem) iter.next();

    if(item.isFormField()) {
    if(item.getFieldName().equals("id")) {
    id = Integer.parseInt(item.getString());
    }
    }

    // 忽略其他不是文件域的所有表单信息
    if (!item.isFormField()) {
    String name = item.getName();
    long size = item.getSize();
    if ((name == null || name.equals("")) && size == 0)
    continue;
    Matcher m = p.matcher(name);
    boolean result = m.find();
    if (result) {
    for (int temp = 0; temp < errorType.length; temp++) {
    if (m.group(1).endsWith(errorType[temp])) {
    throw new IOException(name + ": wrong type");
    }
    }
    try { // 保存上传的文件到指定的目录 // 在下文中上传文件至数据库时,将对这里改写
    item.write(new File(uploadPath+ id + ".jpg"));

    out.print(name + "&nbsp;&nbsp;" + size + "<br>");
    res.setHeader("Refresh","3;URL=../bbs/userinfo.jsp");
    } catch (Exception e) {
    out.println(e);
    } } else {
    throw new IOException("fail to upload");
    }
    }
    }
    } catch (IOException e) {
    out.println(e);
    } catch (FileUploadException e) {
    out.println(e);
    } } }
      

  3.   

    路径你就放到开发的web工程的存储文件中,然后按你当前页面的路径写那个图片存储位置的相对路径就可以了,格式我不懂为什么会变。。
      

  4.   

    相对路径,可以将文件的后缀名改成.jpg
      

  5.   


    很简单的2个问题:
    1.你可以把上传的图片上传到你的tomcat服务器的工程下面。不要写出绝对路径;
    2.一般上传文件都有一个后缀,你那个是强制所有文件的后缀是jpg的,你可以设为默认的后缀;
    也就是上传的时候的后缀。
      

  6.   

    copy code: private static void copy(File src, File dst) {
            try {
               InputStream in = null ;
               OutputStream out = null ;
                try {                
                   in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
                   out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
                    byte [] buffer = new byte [BUFFER_SIZE];
                    while (in.read(buffer) > 0 ) {
                       out.write(buffer);
                   }
               } finally {
                    if ( null != in) {
                       in.close();
                   }
                    if ( null != out) {
                       out.close();
                   }
               }
           } catch (Exception e) {
               e.printStackTrace();
           }
       }
    //取文件后缀名   
        private static String getExtention(String fileName) {
         System.out.println(fileName+"  Filename");
            int pos = fileName.lastIndexOf(".");
            return fileName.substring(pos);
       }
    //执行(/UploadImages  不论项目怎么移动图片始终在其项目的UploadImages文件夹内)
       @Override
        public String execute()     {        
       System.out.println(contentType+"  contentType");
           imageFileName = new Date().getTime() + getExtention(fileName);
           System.out.println(ServletActionContext.getServletContext().getRealPath( "/UploadImages"));
           File imageFile = new File(ServletActionContext.getServletContext().getRealPath( "/UploadImages" ) + "/" + imageFileName);
           copy(myFile, imageFile);
            return SUCCESS;
       }