Ext & Java 上存图片 Demo

时间:2024-03-24 09:03:50

Ext & Java 上存图片 Demo

Ext

<html>
    <head>
         <script id="microloader" type="text/javascript" src="ext-all-debug.js"></script>
         <link rel="stylesheet" type="text/css" href="ext-all.css">
    </head>
    <script type="text/javascript">
        Ext.require([
            'Ext.*'
        ]);

        Ext.onReady(function() {
            Ext.create('Ext.form.Panel', {
                title:'upload',
                width: 400,
                layout: {
                    type: 'vbox',
                    align : 'stretch'
                },
                items:[{
                    xtype: 'fieldset',
                    title: '上存图片',
                    items: [{
                        xtype: 'filefield',
                        fieldLabel: "MyPhoto:",
                        name: 'photo',
                        accept: 'image'
                    },{
                        xtype: 'textfield',
                        fieldLabel: 'param',
                        name: 'test'
                    }],
                }],
                buttons:[{
                    text:'upload',
                    handler:function(btn){
                        var formPanel = btn.up('form');
                        formPanel.submit({
                            url: '../controller/app/uploadFile',
                            waitMsg: 'Uploading file...',
                            params: formPanel.getValues(),
                            timeout: 120,
                            success: function(form, action) {

                            },
                            failure: function(form, action) {

                            }
                        });
                    }
                }],
                renderTo: Ext.getBody()
            });
        })

    </script>
    <body>

    </body>
</html>

Java

@Controller
@RequestMapping("app")
public class WebController {
    @RequestMapping(value = "uploadFile", method = RequestMethod.POST)
    public String uploadFile(
            MultipartHttpServletRequest request,
            HttpServletResponse response,
            String test) throws Exception{

        String userAgent = request.getHeader("User-Agent");
        log.info("userAgent: " + userAgent);
        Iterator<String> its = request.getFileNames();
        if(its.hasNext()){
            MultipartFile file = request.getFile(its.next());
            String name = file.getOriginalFilename();
            byte[] bytes = null;
            if (name != null) {
                bytes = file.getBytes();
            }
            //创建输出流
            FileOutputStream outStream = new FileOutputStream("d://" + name);
            //写入数据
            outStream.write(bytes);
            //关闭输出流
            outStream.close();
        }
        return "success";
    }
}