有文件上传的例子吗??

解决方案 »

  1.   

    用Struts的form 实现   很简单的!!!
    把表单封装成form对象,在 Action里从form里面读取。
    form代码:
    /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClass.vtl
     */
    package com.hhaccp.hr.web.form;
    import org.apache.struts.action.ActionForm;import org.apache.struts.upload.FormFile;/** 
     * MyEclipse Struts
     * Creation date: 04-10-2010
     * 
     * XDoclet definition:
     * @struts.form name="pictureForm"
     */
    public class PictureForm extends ActionForm {
    /*
     * Generated Methods
     */ /**
     * 
     */
    private static final long serialVersionUID = -7396780480884393201L; /** 
     * Method validate
     * @param mapping
     * @param request
     * @return ActionErrors
     */
    private FormFile pictureForm; public FormFile getPictureForm() {
    return pictureForm;
    } public void setPictureForm(FormFile pictureForm) {
    this.pictureForm = pictureForm;
    }}
    Action代码:
    String pictureForm=request.getParameter("pictureForm");
    if(pictureForm!=null){
    PictureForm pic=(PictureForm) form; 
    byte[] file=pic.getPictureForm().getFileData();//把路径变成文件流对象
    String pathName=request.getRealPath("/")+"file/head/";//创建上传路径
    String picName=pic.getPictureForm().getFileName();//获取图片路径
    File saveFile=new File(pathName+picName);//创建文件
    OutputStream os=new FileOutputStream(saveFile);//把文件添加进文件流
    os.write(file);//写入文件
    os.close();//关闭文件流
    humanFile.setPicture(pic.getPictureForm().getFileName());
    }else{
    humanFile.setPicture("dfhead.jpg");
    }我的是上传图片!!!
      

  2.   

    upload.java
    package action;import java.io.*;import com.opensymphony.xwork2.ActionSupport;public class upload extends ActionSupport {
      private File upload;
      private String uploadFileName; //固定的名字不能乱起
      private String uploadContentType;//固定的名字不能乱起
      
      public String execute() throws IOException{
      InputStream is = null;
      OutputStream os = null;
      byte buffer[] = new byte[8192];
      int count=0;
      try{
      is = new FileInputStream(upload);
      os = new FileOutputStream("d:\\java\\"+uploadFileName);
      while((count=is.read(buffer))>0){
      os.write(buffer,0,count);
      }
      
      }catch(Exception e){
      e.printStackTrace();
      }finally{
      os.close();
      is.close();
      }
      
      return SUCCESS;
      }
      
    public File getUpload() {
    return upload;
    }
    public void setUpload(File upload) {
    this.upload = upload;
    }
    //public String getFileName() {
    // return fileName;
    //}
    //public void setFileName(String fileName) {
    // this.fileName = fileName;
    //}
    public String getUploadContentType() {
    return uploadContentType;
    }
    public void setUploadContentType(String uploadContentType) {
    this.uploadContentType = uploadContentType;
    }public String getUploadFileName() {
    return uploadFileName;
    }public void setUploadFileName(String uploadFileName) {
    this.uploadFileName = uploadFileName;
    }
      
    }upload.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
    <%@taglib prefix="s" uri="/struts-tags" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>My JSP 'upload.jsp' starting page</title>
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->  </head>
      
      <body>
        <s:form action="upload" namespace="/" method="post" enctype ="multipart/form-data">
          <s:file name="upload" label="请输入要上传的文件"></s:file>
          <s:submit value="上传"></s:submit>
        </s:form>
      </body>
    </html>
    测试成功.
      

  3.   

    这里有个例子:
    http://download.csdn.net/source/1574104
      

  4.   

    JAVA上传图片源代码:包含检查文件格式、大小等功能。
    struts1.2上传图片源代码(包含检查文件格式、限制文件大小功能)
      

  5.   


    /****************************************************************************
     *更新产品类型图片 xfc 
     ****************************************************************************/
    public ActionForward updateWproducttypePic(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)throws Exception {
    WproducttypeForm wproducttypeForm = (WproducttypeForm)form;
    Integer id = wproducttypeForm.getId();
    if(id!=null && id!=0){
    Wproducttype wproducttype = wproducttypeBiz.findWproducttypeByKey(id);
    //检测是否已有图,有就首先删除原来的图片,没有则直接保存
    File file = new File(request.getRealPath("/upload/img/producttype/"+wproducttype.getUuidFileName()));
    if(file.exists()){
    file.delete();
    System.out.println("-----------已经删除原来的类图");
    }
    FormFile filePic = null;
    Integer indexId = wproducttypeForm.getIndexId();
    if(indexId == 1) filePic = wproducttypeForm.getFile1();
    if(indexId == 2) filePic = wproducttypeForm.getFile2();
    if(indexId == 3) filePic = wproducttypeForm.getFile3();
    if(indexId == 4) filePic = wproducttypeForm.getFile4();
    if(indexId == 5) filePic = wproducttypeForm.getFile5();
    if(indexId == 6) filePic = wproducttypeForm.getFile6();
    //这里处理图片保存
    String fileName = UUIDHelper.getUUID()+"."+FileHelper.getFileSuffix(filePic.getFileName());
    boolean result = false;
    String filePath = request.getRealPath("/upload/img/producttype/"+fileName);
    try {
    FileOutputStream fileOutput = new FileOutputStream(filePath);
            fileOutput.write(filePic.getFileData());
            fileOutput.flush();
            fileOutput.close();
            result = true;
    } catch (Exception e) {
    e.printStackTrace();
    result=false;
    }
    if(result){
    wproducttype.setUuidFileName(fileName);
    wproducttype.setFileName(filePic.getFileName());
    boolean result2 = wproducttypeBiz.updateWproducttype(wproducttype);
    if(result2)reportErrors(request, "<font color='blue'>更新成功!</font>",false);
    else       reportErrors(request, "<font color='red' >更新失败!</font>",false);
    }
    }else{
    reportErrors(request, "<font color='red'>对不起,您要数据不存在!或者数据已丢失!</font>",false);
    }
    return findWproducttypePage(mapping, form, request, response);
    }