文件上传下载

上传:文件 保存再本地计算机中
//上传文件
public boolean atta(MultipartFile[] files,ManagementSpcy management,String status,String approvalId,HttpServletRequest req) throws IOException {
Map<String,String> map=new HashMap<String,String>();
String user_id = (String) req.getSession().getAttribute(“user_id”);
String roleCode = RoleCodeUtil.getRoleCode(req);
Enclosure e = new Enclosure();
if(files != null && files.length > 0){
for(int i = 0;i<files.length;i++){
MultipartFile file = files[i];
if(file != null && file.getSize() > 0){
String fm =file.getOriginalFilename();
File filey = new File(“C:/”+fm);
if (!filey.exists()) {
filey.createNewFile();//如果不存在就创建
}
//2.保存到本地
SaveFileFomInputStream(file.getInputStream(),filey);
String pathx = filey.getPath();
//3.获得本地文件路径:filepath
String filepath = pathx;
File file1 = new File(filepath);
String filename = file1.getName();
String filePath = file1.getPath();
InputStream inputStream = new FileInputStream(filePath);
byte[] data =new byte[]{};
data = inputStreamToByte(inputStream);
//e.setId(GetUuidUtil.getUUID());
e.setContent(data);
e.setName(fm);
e.setNo(management.getNo());
e.setBak1(status);
e.setBak2(approvalId);
e.setBak3(management.getManagementId());
e.setBak4(user_id);
mapper.insertSelective(e);
inputStream.close();
file1.delete();
}else{
return false;
}

		}
		
	}
	
	map.put("msg", "信息新增成功");
	map.put("success", "success");
	
	return true;
}

jsp:

								<input class="form-control" id="category" name="category" data-toggle="city-picker"
									   style="height: 34px;font-size: 14px;" type="hidden" value="" />
							
						
							<div class="col-xs-12 col-sm-6 col-md-6">
							<label class="label-item single-overflow pull-left"
								   title="处置单号:">处置单号:</label>
							<div class=" input-group" style=" width: 100%;">
								<input class="form-control" readonly="readonly" id="no1" name="no" data-toggle="city-picker"
									   style="height: 34px;font-size: 14px;" type="text" value="" />
							</div>
						</div>
						<div class="col-xs-12 col-sm-3 col-md-3">
							<label class="label-item single-overflow pull-left"
								   title="关区名称:">关区名称:</label>
							<div class=" input-group" style=" width: 100%;">
								<input class="form-control" readonly="readonly" id="bak1" name="bak1" data-toggle="city-picker"
									   style="height: 34px;font-size: 14px;" type="text" value="${ sessionScope.path_name }" />
							</div>
						</div>
						<div class="col-xs-12 col-sm-3 col-md-3">
							<label class="label-item single-overflow pull-left"
								   title="当前操作人姓名:">当前操作人姓名:</label>
							<div class=" input-group" style=" width: 100%;">
								<input class="form-control" readonly="readonly" id="firstPersonName1" name="firstPersonName" data-toggle="city-picker"
									   style="height: 34px;font-size: 14px;" type="text" value="${sessionScope.username}" />
								<input name="text" type="hidden" id="state" value="4" name="state">
								<input name="text" type="hidden" id="finalState" value="4" name="finalState">
							</div>
						</div>
						<div class="col-xs-12 col-sm-6 col-md-6">
							<label class="label-item single-overflow pull-left"
								   title="当前操作时间:">当前操作时间:</label>
							<div class="input-group" style=" width: 100%;">
								<input type="text" id="startTime" class="form-control form-size date" data-toggle="city-picker" style="height: 34px;font-size: 14px;"    name="startTime">
							</div>
						</div> 
						<div class="col-xs-12 col-sm-6 col-md-6">
							<label class="label-item single-overflow pull-left"
							   title="关区名称:"><font style="color:red;">上一步操作人:</font></label>
						    <div class=" input-group" style=" width: 100%;">
								<input class="form-control" readonly="readonly" id="bak3" name="bak3" data-toggle="city-picker"
								   style="height: 34px;font-size: 14px;border:1px solid red;" type="text"  />
							</div>
						</div>
						<div class="col-xs-12 col-sm-6 col-md-6">
							<label class="label-item single-overflow pull-left"
								   title="上一步操作环节:"><font style="color:red;">上一步操作环节:</font></label>
							<div class=" input-group" style=" width: 100%;">
								<input class="form-control" readonly="readonly" id="bak4" name="bak4" data-toggle="city-picker"
									   style="height: 34px;font-size: 14px;border:1px solid red;" type="text"  />
							</div>
						</div>
						<div class="col-xs-12 col-sm-6 col-md-6">
							<label class="label-item single-overflow pull-left"
								   title="上一步附件:"><font style="color:red;">上一步附件:</font></label>
							<div id="previousEnclosureId" class=" input-group" style=" width: 100%;">
								<!-- <input class="form-control" readonly="readonly"  name="previousEnclosure" data-toggle="city-picker"
									   style="height: 34px;font-size: 14px;" type="text"  /> -->
							</div>
						</div>
						 <div class="col-xs-12 col-sm-12 col-md-12">
							<label class="label-item single-overflow pull-left"
								   title="上一步审批意见:"><font style="color:red;">上一步审批意见:</font></label>
							<div class=" input-group" style=" width: 100%;">
								<textarea style="border:1px solid red;" class="form-control" rows="4" cols="86" id="bak5" name="bak5" data-toggle="city-picker" readonly="readonly"/></textarea>
							</div>
						</div> 
						<div class="col-xs-12 col-sm-12 col-md-12">
							<label class="label-item single-overflow pull-left"
								   title="意见:">意见:</label>
							<div class=" input-group" style=" width: 100%;">
								<textarea class="form-control" rows="4" cols="86" id="suggestion1" name="suggestion" data-toggle="city-picker"/></textarea>
							</div>
						</div>
						
						<div class="col-xs-12 col-sm-12 col-md-12">
							<label class="label-item single-overflow pull-left"
								   title="上传文件:">上传文件:</label>
							<div class=" input-group" style=" width: 100%;">
									<input name="files" id="file1" type="file" value="" style="float:left;width:200px;">
									<input name="files" id="file2" type="file" style="float:left;width:200px;">
									<input name="files" id="file3" type="file" style="float:left;width:200px;">
							</div>
						</div>
						
						
					</form>

有个文件上传的字样
js:
//入库
function openRetrial()
{
if(KaTeX parse error: Expected 'EOF', got '#' at position 3: ("#̲suggestion1").v…("#endTime").val().length == 0 && $("#startTime").val().length == 0){
toastr.error(“请填写时间”);
return;
}

	if($("#endTime").val().replace(/-/g,"") <= $("#startTime").val().replace(/-/g,"")){
		toastr.error("开始时间大于结束时间");
		return;
	}*/
  var row = $("#dataTableSameName").bootstrapTable("getSelections");
  console.log(row);
  var curWwwPath = window.document.location.href;
  var pathName = window.document.location.pathname;
  var pos = curWwwPath.indexOf(pathName);
  var localhostPath = curWwwPath.substring(0,curWwwPath.indexOf("/",pos+1)+1);
  var data = new FormData($('#detailForm')[0]);
  $.ajax({
  	  async:true,//是否异步
  	  cache:false,//	
  	  url:localhostPath+"/sameNameGoodsRetrialAction/sameNameOpenRetrial?managementId=" +row[0].managementId+"&no="+row[0].no,
      dataType: 'json',
      type:'post',
      contentType:false,
      processData:false,
      data:data,
    success:function(data){
    	$("#addmodals").modal("hide");
    	toastr.success("操作成功");
    	//$("#dataTableSameName").bootstrapTable("refresh");
    	ReloadTable();
    },
    errror:function(){
    	toastr.error("操作失败");
    	$("#dataTableSameName").bootstrapTable("refresh");
    }
});

}

即可上传

下载:

@RequestMapping(value = {"/down"})
//@ResponseBody
public void xz(HttpServletResponse resp,Model model,HttpServletRequest request) throws IOException {
byte data[] = new byte[]{};
File file =null;
FileOutputStream fos = null;
FileInputStream in = null;
//a是id,根据id查所有数据
String id = request.getParameter(“id”);
Enclosure rw= mapper.selectByPrimaryKey(id);
String filename = rw.getName();

	if (rw!=null ) {
		data = rw.getContent();
	}
	filename = URLEncoder.encode(filename,"UTF-8");
	resp.reset();
	resp.setContentType("application/octet-stream; charset=GBK");
	resp.setHeader("Content-Disposition", "attachment; filename=" + filename);
	
	
	/*file = new File(getValue("url")+rw.getFileLength());
	File fileParent = file.getParentFile();
		if (!fileParent.exists()) {
			fileParent.mkdirs();//如果不存在就创建
		}
		//file.createNewFile();
		fos = new FileOutputStream(file);
		int size = 0;
		if (data.length>size) {
			fos.write(data,0,data.length);
		}
		fos.close();*/
		/*in = new FileInputStream(file);
		BufferedInputStream bufferedInputStream = new BufferedInputStream(in);*/
		OutputStream out = resp.getOutputStream();
		BufferedOutputStream bufferpublic class Enclosure {
private String id;

private String no;

private String name;

private String bak1;    //审核状态 final_state

private String bak2;    //审批流主键

private String bak3; 	//management表的主键

private String bak4;	//用户id

private String bak5;	//角色roleCode

private String bak6;	

private byte[] content;edOutputStream = new BufferedOutputStream(out);
		if (data.length>0) {
			bufferedOutputStream.write(data,0,data.length);
		}
		bufferedOutputStream.close();

		//map.put("msg", "下载成功,在'"+getValue("url")+"'文件夹中查看!");
		//map.put("success", "success");
}

bean:

public class Enclosure {
private String id;

private String no;

private String name;

private String bak1;    //审核状态 final_state

private String bak2;    //审批流主键

private String bak3; 	//management表的主键

private String bak4;	//用户id

private String bak5;	//角色roleCode

private String bak6;	

private byte[] content;

js:

function downloads(id){
window.open("…/trialAction/down?id=" + id);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值