@Controller
@RequestMapping("/file")
public class FileController {
@Value("#{configProperties['downloadPath']}")
private String downloadPath;
@RequestMapping(value = "/download", method = RequestMethod.GET)
public void download(String fileName, HttpServletRequest request, HttpServletResponse response) throws IOException {
String transcoding = new String(fileName.getBytes("iso8859-1"),"utf-8");
String downloadName = URLEncoder.encode(transcoding, "UTF-8");
response.setCharacterEncoding("utf-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;fileName=" + downloadName);
InputStream is = null;
OutputStream os = null;
try {
String path = request.getServletContext().getContextPath();
is = new FileInputStream(new File(downloadPath + transcoding));
os = response.getOutputStream();
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) > 0) {
os.write(b, 0, length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
os.close();
is.close();
}
}
}
就很普通的一段下载代码,在PC没问题的,但是在手机点击下载(txt/jpg),就会马上解析然后展示在页面上,现在希望是实现在手机端点击下载的时候提示下载到手机本地,之前没做过在手机端的下载,大家指点下谢谢。