Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package net.nan21.dnet.core.web.controller.upload;
  
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  import  javax.servlet.http.HttpServletRequest;
  import  javax.servlet.http.HttpServletResponse;
  
 
 import  org.codehaus.jackson.map.ObjectMapper;
 import  org.springframework.beans.factory.NoSuchBeanDefinitionException;
 import  org.springframework.stereotype.Controller;
 import  org.springframework.web.bind.annotation.PathVariable;
 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;
 
 @Controller
 
Generic file upload. Expects an uploaded file, a set of context parameters with names starting with "_p_" and a handler alias to delegate the uploaded file processing to.

Parameters:
handler spring-bean alias of an net.nan21.dnet.core.api.service.IFileUploadService which should process the uploaded file
file Uploaded file
request
response
Returns:
Throws:
Exception
 
 	@RequestMapping(value = "/file-upload/{handler}", method = RequestMethod.POST)
 	@ResponseBody
 	public String fileUpload(@PathVariable("handler"String handler,
 			@RequestParam("file") MultipartFile file,
 			HttpServletRequest request, HttpServletResponse response)
 			throws Exception {
 
 		if (file.isEmpty()) {
 			throw new Exception("Upload was not succesful. Try again please.");
 		}
 
 		this.prepareRequest(requestresponse);
 		// Map<String, String> contextParams = this.collectParams(request);
 		Map<StringObjectresult = new HashMap<StringObject>();
 
 		// try to find a delegate to hand over processing
 
 		this.finishRequest();
 		result.put("success"true);
 		ObjectMapper mapper = getJsonMapper();
 
 		return mapper.writeValueAsString(result);
 	}

Dedicated file upload for data-source CSV imports.

Parameters:
file
request
response
Returns:
Throws:
Exception
 
 	@RequestMapping(value = "/ds-csv-import/{dsName}", method = RequestMethod.POST)
 	@ResponseBody
 	public String dsCsvImport(@PathVariable("dsName"String dsName,
 			@RequestParam("file") MultipartFile file,
 			HttpServletRequest request, HttpServletResponse response)
 			throws Exception {
 
 		if (file.isEmpty()) {
 			throw new Exception("Upload was not succesful. Try again please.");
 		}
 
 		this.prepareRequest(requestresponse);
 
 		this.authorizeDsAction(dsName.substring(0, dsName.length() - 2),
 				"import");
 
 		//Map<String, String> contextParams = this.collectParams(request, "_p_",
 		//		null);
 		Map<StringObjectresult = new HashMap<StringObject>();
 
 		IDsService<?, ?, ?> dsService = this.getServiceLocator().findDsService(
				dsName);
		dsService.doImport(file.getInputStream(), file.getOriginalFilename());
		result.put("success"true);
		ObjectMapper mapper = getJsonMapper();
		return mapper.writeValueAsString(result);
	}
	}
			List<IFileUploadServiceFactoryfileUploadServiceFactories) {
		this. = fileUploadServiceFactories;
	}

Try to locate a file-upload service by its name (spring-bean alias)

Parameters:
dsName
Returns:
Throws:
Exception
			throws Exception {
		IFileUploadService srv = null;
			try {
				srv = sf.create(name);
				if (srv != null) {
					return srv;
				}
catch (NoSuchBeanDefinitionException e) {
				// service not found in this factory, ignore
			}
		}
		throw new Exception(name + "File upload service not found for name "
name + "!");
	}
New to GrepCode? Check out our FAQ X