标签:
//文件下载的话基本上固定写法,重点看是否拿到文件的路径,和判断是否文件存在已经下载的时候文件名的中文乱码问题。
@RequestMapping("downTable")
public void downTabel(String tableId, HttpServletRequest request,
HttpServletResponse response) throws IOException {
if (!tableId.isEmpty() && tableId != null) {
OaTable oaTable = oaTableService.getOaTableById(tableId);
if (oaTable != null) {
File file = new File(request.getRealPath(oaTable.getTableFilePath()));
if (file.exists()) {//文件存在 进行文件的下载
String filename = oaTable.getTableFileName();
String downLoadName = new String(filename.getBytes("gbk"), "iso8859-1");//解决中文下载乱码问题
response.setContentType("octets/stream");
response.addHeader("Content-Type", "text/html; charset=utf-8");
response.setHeader("Content-disposition","attachment;filename=" + downLoadName);
//以下就是文件输出代码了
try {
FileInputStream fileInputStream = new FileInputStream(file);
OutputStream out = response.getOutputStream();
int i = 0;
while ((i = fileInputStream.read()) != -1) {
out.write(i);
}
out.close();
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
上传的時候不知道怎麽MVC 慘不過來參數 只好用request接收。。吧表單提交路徑改成文秘需要的參數GET請求
//前台页面
<form id="form1" name="form1" action="${pageContext.request.contextPath}/oaTable/saveTable.do" method="post" enctype="multipart/form-data" >
<div style="text-align: center;"><h3>工作表格上传</h3></div>
<table class="ctable" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<th width="20%">位 置 </th>
<td>
<select class="ctxt1" name="tableSort" id="tableSort" >
<option value="1" selected="selected">默认放在第一位</option>
<c:forEach items="${list }" varStatus="vs">
<option value="${vs.count+1}">${vs.count+1}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<th>表格名称</th>
<td>
<input class="ctxt1" id="tableName" name="tableName" type="text" style="width: 260px;" id="roomTel" maxlength="20">
<span id="tableName_span" style="color: red;font-size: 16px;"></span>
</td>
</tr>
<tr>
<th>文件名称</th>
<td>
<input type="text" id="fileaddress" class="ctxt1" style="width: 400px;" readonly="readonly"/>
<input type="file" id="filename" name="filename" onchange="fileaddress.value=this.value;"
style="position:absolute;opacity:0;filter:alpha(opacity=0);width:55px;height:25px;" />
<input type="button" value="浏览" class="cbtn cbtn_1"/>
</td>
</tr>
<tr>
<th></th>
<td>
<input type="button" value="提交" onclick="doSubmit();" class="cbtn cbtn_1"/>
<input type="button" value="关闭" class="cbtn cbtn_7" id="close"/>
</td>
</tr>
</table>
</form>
function doSubmit(){
var fileName = $("#filename").val();
if (fileName == "") {
alert("请选择您需要上传的文件!");
}else{
checkTableName();
if(tableNameFlag==true){
var tableSort = $("#tableSort").val();
var tableNamecn = $("#tableName").val();
var url="${pageContext.request.contextPath}/oaTable/saveTable.do?tableSort="+tableSort+"&tableName="+tableNamecn;
//将表单的action的URL更改为我们希望提交的URL
document.form1.action=url;
$("#form1").submit();
}
}
}
//后台的上传方法
@RequestMapping("/saveTable")
public String saveTable(HttpServletRequest request, Model model)
throws Exception {
Integer tableSort;
String tableName;
String name = "";// 文件名称
String savePath = "";// 用于保存到数据库的路径
String uploadPath = request.getRealPath("/oatable/upload");
;// 用于上传的路径
String tempPath = request.getRealPath("/oatable/temp");
;// 用于存放临时文件
final int flushSize = 1024 * 512;// 用于缓冲区大小
final long fileSize = 40 * 1024 * 1024;// 文件最大大小
File uploadFile = new File(uploadPath);
if (!uploadFile.exists()) {
uploadFile.mkdir();
}
File tempFile = new File(tempPath);
if (!tempFile.exists()) {
tempFile.mkdir();
}
boolean isMultipart = FileUpload
.isMultipartContent(new ServletRequestContext(request));
if (!isMultipart) {
System.out.println(isMultipart + "错误!!!!!");
return "";
}
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置缓冲区
factory.setSizeThreshold(flushSize);
factory.setRepository(new File(tempPath));
ServletFileUpload upload = new ServletFileUpload(factory);
// 设置文件最大大小
upload.setSizeMax(fileSize);
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
// 判断是否是file表单元素
if (!item.isFormField()) {
// 获得文件路径和名称
name = item.getName();
name = name.substring(name.lastIndexOf(File.separator) + 1,
name.length());// 获得文件名
savePath = "/oatable/upload/" + name;
item.write(new File(uploadPath + "/" + name));
}
}
// 保存table到数据库
tableName = request.getParameter("tableName");
tableSort = Integer.parseInt(request.getParameter("tableSort"));
OaTable oaTable = new OaTable();
oaTable.setTableId(StrUtil.GetUUID(null));
oaTable.setTableNameCn(tableName);
oaTable.setTableFileName(name);
oaTable.setTableFilePath(savePath);
oaTable.setTableSort(tableSort);
oaTable.setTableDate(new Date());
// 在保存表格之前先吧大于等于tableSort的表格都增加1 这样就空出来了这个位置
if (oaTableService.updateTableBySort(tableSort, OATABLE_ADD)) {
if (oaTableService.saveOaTable(oaTable)) {
System.out.println("保存表格成功");
return index(model);
} else {
System.out.println("保存表格失败");
return "error";
}
} else {
System.out.println("保存表格失败");
return "error";
}
}
标签:
原文地址:http://www.cnblogs.com/whwu123/p/5953955.html