我通过一下方式可以浏览图片并获得图片在本地的路径如下:<td><input name="xmlFile" id="xmlFile" type="file" class="aibi_input_file" contenteditable="false"></td>
但是想实现将获得本地路径重新组装程新的路径,并将浏览的图片保存到指定的新路径里,请问这个怎么实现,最好有代码,谢谢!

解决方案 »

  1.   

    这个啊,commons-upload
    代码如下try{
    DiskFileUpload fu = new DiskFileUpload();
            // 设置最大文件尺寸,这里是50MB
            fu.setSizeMax(50194304);
            // 设置缓冲区大小,这里是4kb
            fu.setSizeThreshold(4096);
            int size= request.getContentLength();
    if(size>50000000){
    request.setAttribute("msg", "发送失败!文件大小不能超过50M!");
    request.getRequestDispatcher("bbs_add.jsp").forward(request, response);
    return null;
    }
    FormFile file = bbsForm.getFileContent();// 取得上传的文
    if(!file.getFileName().equals("")){
    SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyyMMdd");// 文件重命名
    Date date = new Date();
    String datename = String.valueOf(bartDateFormat.format(date));
    String hour = String.valueOf(date.getHours());
    String min = String.valueOf(date.getMinutes());
    String second = String.valueOf(date.getSeconds());
    String f = bbsForm.getFileContent().getFileName().substring(0,bbsForm.getFileContent().getFileName().indexOf("."));
    String namefile = f + datename + hour + min + second;
    String filename = null;
    InputStream stream = file.getInputStream();// 把文件读入
    String filePath = this.getServlet().getServletContext().getRealPath("/")+ "upload\\";
    filename = file.getFileName();
    filename = namefile + filename.substring(filename.indexOf("."));// 保存文件路径和文件名
    OutputStream bos = new FileOutputStream(filePath + "/" + filename);// 建立一个上传文件的路径
    int bytesRead = 0;
    byte[] buffer = new byte[8192];
    tBbs.setFujian(filename);
    while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
    bos.write(buffer, 0, bytesRead);// 将文件写入服务器
    }
    bos.close();
    stream.close();
    }}catch(Exception e){
    request.setAttribute("msg", "文件上传失败!");
    request.getRequestDispatcher("bbs_add.jsp").forward(request, response);
    return null;
    }
      

  2.   

      String f = bbsForm.getFileContent().getFileName().substring(0,bbsForm.getFileContent().getFileName().indexOf("."));//BbsForm里有private FormFile fileContent;bbsForm是public class BbsForm extends ActionForm 
            String namefile = f + datename + hour + min + second;
            String filename = null;
            InputStream stream = file.getInputStream();// 把文件读入
                String filePath = this.getServlet().getServletContext().getRealPath("/")+ "upload\\";  //项目web下面要有upload文件夹
             
      

  3.   

    新建一个form类继承Actionform
    /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClass.vtl
     */
    package com.jnr.web.form;import javax.servlet.http.HttpServletRequest;
    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.upload.FormFile;
    public class BbsForm extends ActionForm {
    /*
     * Generated fields
     */ /** has property */
    private FormFile fileContent;
    /** 
     * Method validate
     * @param mapping
     * @param request
     * @return ActionErrors
     */
    public ActionErrors validate(ActionMapping mapping,
    HttpServletRequest request) {
    // TODO Auto-generated method stub
    return null;
    } /** 
     * Method reset
     * @param mapping
     * @param request
     */
    public void reset(ActionMapping mapping, HttpServletRequest request) {
    // TODO Auto-generated method stub
    }
    public TBbs gettBbs() {
    return tBbs;
    }
    public void setFileContent(FormFile fileContent) {
    this.fileContent = fileContent;
    }}
      

  4.   

    应用common-upload组件上传文件
    你可以自己在网上搜索下这些,我也是参考网上自己做的
      

  5.   

    网上搜java文件上传
    一搜一大把
      

  6.   

    我刚写的可以参考一下:
    http://blog.csdn.net/zxingchao2009/archive/2011/06/09/6534261.aspx
      

  7.   

     String filePath = this.getServlet().getServletContext().getRealPath("/")+ "upload\\";  //项目web下面要有upload文件夹这句是关键,这样就是绝对路径了,也就是项目下。