package jp.dip.arimodoki.cntl;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
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.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import jp.dip.arimodoki.common.CConst;
import jp.dip.arimodoki.common.CheckBrowserIf;
@RestController
public class FileDownloadAjax implements CConst {
@Autowired
CheckBrowserIf checkBrowser;
@RequestMapping(value = "/filedownloadajax")
public ModelAndView filedownloadajax() {
return new ModelAndView("filedownloadajax");
}
@RequestMapping(value = "/upload_ajax",
method = RequestMethod.POST,
produces = "text/html;charset=UTF-8"
)
public String uploadajax(
HttpSession session,
@RequestParam("uploadfile") MultipartFile uploadfile
) throws Exception {
session.setAttribute("uploadfile", uploadfile);
return "{\"status\":0,\"message\":\"Complete.\"}";
}
@RequestMapping(value = "/downloadajax")
public void downloadajax(
HttpSession session,
HttpServletRequest req,
HttpServletResponse res
) throws Exception {
MultipartFile uploadfile = (MultipartFile)session.getAttribute("uploadfile");
int blowser = checkBrowser.getBrowser(req.getHeader("user-agent"));
String fname = URLEncoder.encode(uploadfile.getOriginalFilename(), "UTF-8");
if(blowser == CheckBrowserIf.BROWSER_FF) {
res.setHeader("Content-Disposition","attachment; filename*=¥"UTF-8''"+fname+"¥"");
} else {
res.setHeader("Content-Disposition","attachment; filename=¥""+fname+"¥"");
}
res.setContentLength((int) uploadfile.getSize());
FileCopyUtils.copy(uploadfile.getInputStream(), res.getOutputStream());
session.removeAttribute("uploadfile");
return;
}
}
|