日期:2014-05-17  浏览次数:20719 次

关于上传和下载的问题
我现在做一个Struts1+hibernate多附件的上传和下载程序 不知道如何下手,谁能指点下迷津,很急啊 最好有完整的代码示例,谢谢了啊

------解决方案--------------------
楼主可参考这个:http://www.blogjava.net/hijackwust/archive/2007/08/22/138598.html
------解决方案--------------------
使用jquery-uploadify插件,网上的实例:http://www.jb51.net/article/21888.htm

Java code

<script type="text/javascript">
$(document).ready(function() {
    $("#uploadify").uploadify({
        'uploader'       : 'js/uploadify.swf',
        'script'         : 'UploadServlet',//后台处理程序的相对路径
        'cancelImg'      : 'images/cancel.png',
        'folder'         : 'uploads',
        'queueID'        : 'fileQueue',
        'auto'           : true,//是否自动上传。true为自动上传
        'multi'          : false//是否可以进行多文件上传。只是设置在框框内显示的文件的个数。true为可以显示多个
    });
});
</script>
</head>

<body>
<div id="fileQueue"></div>
<input type="file" name="uploadify" id="uploadify" /><br>

<p>
<a href="javascript:$('#uploadify').uploadifyUpload()">上传</a>|
<a href="javascript:jQuery('#uploadify').uploadifyClearQueue()">取消上传</a></p>
</body>
</html>

------解决方案--------------------
上传下载 和hibernate没什么关系,只有保存实体数据时才有!

多附件上传,无非处理form提交时判断客户端的muti data元素,控制好文件类型的过滤,大小等就可以了
------解决方案--------------------
import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;


public class FilesUploadAction {
private File[] image;
private String[] imageFileName;

public File[] getImage() {
return image;
}

public void setImage(File[] image) {
this.image = image;
}

public String[] getImageFileName() {
return imageFileName;
}

public void setImageFileName(String[] imageFileName) {
this.imageFileName = imageFileName;
}

public String addUI(){
return "success";
}

public String execute() throws Exception{

String realpath = ServletActionContext.getServletContext().getRealPath("/images");
if(image!=null){
File savedir = new File(realpath);
if(!savedir.exists()) savedir.mkdirs();
for(int i = 0 ; i<image.length ; i++){
File savefile = new File(savedir, imageFileName[i]);
FileUtils.copyFile(image[i], savefile);
}
ActionContext.getContext().put("message", "上传成功");
}
return "success";
}
}

------解决方案--------------------
Java code
package com.cj.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport{

//    一个文件域对应三个字段
    private File pic;//对应jsp中文件域的name
    private String picFileName;//对应文件的名字
    private String picContentType;//文件类型
    private String savePath;//保存路径,使用动态配置
    
    @Override
    public String execute() throws Exception {
        // TODO 自动生成方法存根
        String pfile = getSavePath() + "\\" + picFileName;//设置保存路径
        FileInputStream fis = new FileInputStream(pic);
        byte b[] = new byte[fis.available()];//设置最大数组空间
        FileOutputStream fos = new FileOutputStream(pfile);
        fis.read(b);
        fos.write(b);
        fis.close();
        fos.close();
        return SUCCESS;
    }
    public File getPic() {
        return pic;
    }
    public void setPic(File pic) {
        this.pic = pic;
    }
    public