Administrator
2022-09-14 58d006e05dcf2a20d0ec5367dd03d66a61db6849
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.mandi.servlet.controller;
 
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
 
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.mandi.common.SessionMethod;
 
 
/** 
 * @author mengly 
 * @version 创建时间:2016年4月15日 下午7:28:44 
 * 类说明 
 */
@Controller("fileupload")
@RequestMapping(value="/file/download",method={RequestMethod.GET,RequestMethod.POST})
public class Fileupload {
    @Resource(name="multipartResolver")
    private CommonsMultipartResolver cmr;
    @RequestMapping(value = "/up.htm", method = {RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    public String up(HttpServletRequest r,HttpServletResponse re)
    {
        if(cmr.isMultipart(r))
        {
            @SuppressWarnings("unused")
            MultipartHttpServletRequest mhsr=(MultipartHttpServletRequest) r;
        }else{
            
        }
        return null;
    }
    @RequestMapping(value = "/ups.htm", method = {RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    public String ups(HttpServletRequest r,HttpServletResponse re,@RequestParam("file") MultipartFile[] mfs)
    {
        JsonArray ja=new JsonArray();
        Gson g=new Gson();
        for(MultipartFile mf:mfs){
            String rf=r.getServletContext().getRealPath("/imgs/adsense/imgs/");
            File f=new File(rf);
            if(!f.exists())
                f.mkdirs();
            if(!rf.equals(File.separator)){
                rf+=File.separator;
            }
            String tname=UUID.randomUUID().toString()+mf.getOriginalFilename();
            Path p=Paths.get(rf+tname);
            JsonObject jo=new JsonObject();
            try {
                Files.copy(mf.getInputStream(), p);
                jo.addProperty("url", p.toString());
                jo.addProperty("size", "1024");
                jo.addProperty("name1", mf.getOriginalFilename());
                ja.add(jo);
            } catch (IOException e) {
                e.printStackTrace();
                SessionMethod.writeresp(re, g.toJson(jo));
                return null;
            }
            
        }
        SessionMethod.writeresp(re, g.toJson(ja));
        return null;
    }
}