Skip to content

Instantly share code, notes, and snippets.

@relax-more
Last active December 12, 2015 07:38
Show Gist options
  • Save relax-more/4737892 to your computer and use it in GitHub Desktop.
Save relax-more/4737892 to your computer and use it in GitHub Desktop.
[java] file upload sample *一部抜粋
<beans>
<bean id="serviceProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:service.properties</value>
</list>
</property>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="300000000"/>
</bean>
</beans>
# server environment
environment=local
anyFile.uploadpath=/any/path
@Controller
@RequestMapping(value = "/anyFile")
public class UploadController {
final static Logger LOGGER = LoggerFactory.getLogger(UploadController.class);
@Value(value = "#{serviceProperties['anyFile.uploadpath']}")
String filePath;
/**
* スタンダードな例
* @param request
* @param response
* @throws Exception
*/
@SuppressWarnings("unused")
@RequestMapping(value = "/upload.mvc")
public void upload(HttpServletRequest request, HttpServletResponse response) throws Exception {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1426);
ServletFileUpload upload = new ServletFileUpload(factory);
LOGGER.info("request_received.");
BufferedReader reader = null;
InputStream is = null;
FileItem uploadedFile = null;
try {
for (Object data : upload.parseRequest(request)) {
if (data != null) {
if ("file".equals(item.getFieldName())) {
uploadedFile = item;
}
}else{
LOGGER.debug("data is null");
}
}
if (uploadedFile != null) {
LOGGER.info("fie save path:" + filePath + "/" + uploadedFile.getName());
uploadedFile.write(new File(filePath, uploadedFile.getName()));
} else {
LOGGER.info("uploadedFile :" + uploadedFile);
}
} catch (Exception e) {
LOGGER.error(e.getMessage());
e.printStackTrace();
throw e;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// nothing todo
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// nothing todo
}
}
}
}
/**
* springのMultipartFileを利用した例
* @param multipartFile
* @throws IllegalStateException
* @throws IOException
*/
@RequestMapping(value = "/multi_upload")
public void uploadMulti(@RequestParam("file") MultipartFile multipartFile) throws IllegalStateException, IOException {
LOGGER.info("upload in multipart.");
String fileName;
File file;
LOGGER.info("file save path:" + filePath + "/" + multipartFile.getName());
file =new File(filePath, multipartFile.getName());
LOGGER.debug("multipartFile:" + multipartFile.getName());
if (!multipartFile.isEmpty()) {
multipartFile.transferTo(file);
LOGGER.info("execute transfered");
}
}
}
<VirtualHost *:xxxx>
<中略>
LimitRequestBody 300000000
</VirtualHost>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment