Skip to content

Instantly share code, notes, and snippets.

@oak-tree
Last active November 20, 2016 16:47
Show Gist options
  • Save oak-tree/f36c03ef7e9a28a37ef9481f2c278f77 to your computer and use it in GitHub Desktop.
Save oak-tree/f36c03ef7e9a28a37ef9481f2c278f77 to your computer and use it in GitHub Desktop.
spring upload file
public interface Attachable {
public File getFile();
public String getTitle();
public String getDescription();
public String getRealFileName();
public Double getSize();//double?
public void saveFile(File destination);
}
package somepackagename.here;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import javax.servlet.http.HttpServletResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
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.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MultipartFile;
import org.apache.commons.io.FilenameUtils;
import java.util.UUID;
/**
* api controller for user info . basic info and paging garden
*
* @author oaktree
*
*/
public class AttachmentResource implements Attachable{
private static final Logger LOGGER = LogManager.getLogger(Attachment.class);
private MultipartFile data;
private File file;
private String title;
private String description;
private String realFileName; //generated
/**
* if already bean genereated that return it otherwise generate,save the name return the result
*/
public String getRealFileName(){
return (realFileName == null) ? realFileName=generateRandomUUID(this.data,"") : realFileName;
}
private String generateRandomUUID(MultipartFile file, String prefix){
return prefix + UUID.randomUUID() + "." + FilenameUtils.getExtension(file.getOriginalFilename());
}
}
public void saveFile(File destination) {
//check if file has been saved already, if so return
//newfile = getRealFileName
//copy this.data to newfile
//on anyproblem throw expection.
}
}
package somepackagename.here;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import javax.servlet.http.HttpServletResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
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.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MultipartFile;
/**
* api controller for user info . basic info and paging garden
*
* @author oaktree
*
*/
@Controller
@RequestMapping(value = "api/social/observation")
public class AttachmentController {
private static final Logger LOGGER = LogManager.getLogger(AttachmentController.class);
/**
* update observation domain
*
* @param file
* @param id
* @return the id of the image in system.
* @throws BadCategoryNameException
*/
@RequestMapping(value = "{id}/add/attachment", method = RequestMethod.POST)
@ResponseBody
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void addAttachment(@PathVariable("id") Long id, @RequestParam("file") MultipartFile attachment)
throws BadCategoryNameException {
modle.createAttachment(id, attachment);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!-- Configure the multipart resolver -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:maxUploadSize="1500000" />
</beans>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment