forked from jkxqj/KingIM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileUpload.java
46 lines (42 loc) · 1.71 KB
/
FileUpload.java
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
import java.io.File;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.bson.Document;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.fastjson.JSON;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.nercel.cyberhouse.model.sns.UFile;
import com.nercel.cyberhouse.model.sns.UploadFile;
import com.nercel.cyberhouse.util.MongoUtils;
import net.sf.json.JSONObject;
@Controller
@RequestMapping("sns")
public class FileUpload {
@RequestMapping(value = "/uploadFile", produces = "text/plain; charset=utf-8")
public @ResponseBody String upload(@RequestParam(value = "file", required = false) MultipartFile file,int userId,HttpServletRequest request) {
UploadFile upFile = new UploadFile();
String path = request.getSession().getServletContext().getRealPath("upload/sns/"+userId+"/");
String fileName = file.getOriginalFilename();
String name=UUID.randomUUID().toString()+"---"+fileName;
File targetFile = new File(path,name);
if(!targetFile.exists()){
targetFile.mkdirs();
}
try {
file.transferTo(targetFile);
upFile.setCode(0);
UFile uf=new UFile();
uf.setName(fileName);
uf.setSrc("upload/sns/"+userId+"/"+name);
upFile.setData(uf);
} catch (Exception e) {
e.printStackTrace();
}
return JSON.toJSONString(upFile);
}
}