uploadify上传文件

来源:互联网 发布:python opencv教程 编辑:程序博客网 时间:2024/06/17 22:15

1、上传页面

<divid="queue"></div>

<input id="image_upload"name="image_upload" type="file"multiple="true">

2、设置上传参数

var setting = {

'swf'      : curpath +'/uploadify/uploadify.swf',

'uploader' : 上传后台地址,

'auto'     : false,

 'fileObjName'   : 'file', 

'buttonText' : '选择上传图片',

'fileTypeExts' :'*.jpg;*.jpeg;*.gif;*.png',

'fileTypeDesc' :'*.jpg;*.jpeg;*.gif;*.png',

'fileSizeLimit': '300KB',

'multi' : false,

'successTimeout' : 300,

'onUploadSuccess' : function(f,d,r){

  dijit.byId("uploadFileImageDialog").hide();

p.callback(f,d,r);

} ,

 'onSelect' : function(file) {//文件选择后触发
console.log(file);
}

};

$('#file').uploadify(setting);

3.点击上传时动态修改参数

//$('#file').uploadify("settings","uploader",uploader);

$("#image_upload").uploadify('settings','formData',{'id':id,'description':description,'type':type}); 
$('#file').uploadify('upload');

 

4.将文件存储到数据库

 

public String saveFile(ModelMap model,
MultipartHttpServletRequest request, HttpServletResponse response,
String param,String sysname) throws Exception {
try {
MultipartFile ufile = request.getFile("file");
String fileName = ufile.getOriginalFilename();
   String fileType=originalFileName.substring(fileName .lastIndexOf(".")+1);
    String name =originalFileName.substring(0,fileName .lastIndexOf("."));

DataSource ds = (DataSource)this.ac.getBean("dataSource");

Connection conn = ds.getConnection();

conn.setAutoCommit(false);

PreparedStatement pst  =conn.prepareStatement("insert into image(id,content,name)values(?,?,?)");
//Map<String, String[]> params = request.getParameterMap();

pst.setString(1, id);

pst.setBytes(2, ufile.getBytes());

pst.setString(3,name);

pst.executeUpdate();

conn.commit();

 pst.close();

5.将文件显示到jsp页面

var path=curpath + '后台地址/id='+id+'&method=showImage';
$('#houseimage').attr('src',path);

 File file = service.getFile(param);
  public void showImage(HttpServletRequest request,HttpServletResponse response){    
      response.setCharacterEncoding("utf-8");
     /*response.setContentType("multipart/form-data");*/

 File file =getImageFile(id)
       try {
           InputStream inputStream = newFileInputStream(file);
           OutputStream os = response.getOutputStream();
           
           byte[] b = new byte[2048];
           int length;
           while ((length = inputStream.read(b))> 0) {
               os.write(b, 0, length);
           }
 
           os.close();
 
           inputStream.close();
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }

 

private File getImageFile(String id) {

conn = ds.getConnection();

conn.setAutoCommit(false);

stmt = conn.prepareStatement("selectname,content from image WHERE id = ?");

stmt.setString(1, id);

rs = stmt.executeQuery();

rs.next();

String tempDir = Common.getWebRootPath() +"/temp/image";

String fileDir = tempDir + "/" +id;

FileUtils.forceMkdir(new File(fileDir));

String fileName =rs.getString("NAME");

String filePath = fileDir + "/" +fileName;

File file = new File(filePath);

in =rs.getBlob("CONTENT").getBinaryStream();

out = new FileOutputStream(file);

IOUtils.copy(in, out);

return file;

}

 

原创粉丝点击