文件下载

来源:互联网 发布:今目标软件下载 编辑:程序博客网 时间:2024/06/05 15:37

//要先引入jquery 文件 。

根据单选的不同值下载不同的文件。


<div>
<label><input  type="radio" name="file" value="file1" checked/> file1 </label>
<label><input type="radio" name="file" value="file2"/>file</label>
<label><input type="radio" name="file" value="file3"/> file3 </label>
</div>


<span onclick="down()" class="down">下载文件</span>


<form id="downloadForm" action="${ctx}/file/fileDown">
<input type="hidden" name="fileDown" id="download"/>
</form>

<script>

var downType = function(){
var value = $('in
put[name="file"]:checked').val();
$("#fileDown").val(value);
$("#downloadForm").submit();
};
              

  

</script>



Java代码:

@Controller
@RequestMapping("file")

public class CopyrightController {

@RequestMapping("fileDown")
public void fileDownBybizTypeName(String fileDown,HttpServletRequest request,HttpServletResponse response) throws JspException {
String fileName = "";
String downFileName = "";
switch(fileDown){
case "file1":
fileName = "file.xls";
downFileName = "歌曲申报.xls";
break;
case "MV申报": 
fileName += "song_MV.xls";
downFileName = "MV申报.xls";
break;
case "卡拉OK申报":
fileName += "song_Karaoke.xls";
downFileName = "卡拉OK申报.xls";
break;

try {
response.setContentType("application/octet-stream");
String path = request.getServletContext().getRealPath("/resources/template")+"/";
response.addHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(downFileName, "UTF-8") );
File file = new File(path,fileName);
InputStream is = new FileInputStream(file);
OutputStream os = response.getOutputStream();
int len = -1;
byte [] data = new byte[1024];
while((len = is.read(data)) != -1){
os.write(data, 0, len);
}
os.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
         


}


0 0
原创粉丝点击