关于Java中上传文件与下载文件问题-----同样适用于服务器

一、文件上传

在进行文件上传的时候我们需要注意这么几个点

1、文件上传的时候,表单提交的时候。

<s:form action="UpLoadAction" enctype="multipart/form-data" method="post">
		<s:textfield name="title" label="类型"></s:textfield>
		<s:file name="upload" label="文件"></s:file>
		<s:submit value="提交"></s:submit>
</s:form>

2、文件上传之后,要使用来接受。

private String title;
	private File upload;                //文件
	private String uploadFileName;        //文件名
	private String uploadContentType;    //文件类型
	private String savePath;            //文件路径
	public String getSavePath() {
		return savePath;
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	
	public String execute() throws Exception{
		
		String newName = UUID.randomUUID()+uploadFileName;
		FileOutputStream fos = new FileOutputStream(savePath+"/"+newName);
		FileInputStream fis = new FileInputStream(upload);
		
		byte[] buffer = new byte[1024];
		int len = 0;
		while((len = fis.read(buffer)) > 0) {
			fos.write(buffer,0,len);
		}
		return "success";
	}

3、笔者使用的是Struts2,所以Action配置的时候应该是这样的

<action name="UpLoadAction" class="Nuc.Test.Action.UpLoadAction">
             <!-- 配置fileUpload拦截器 -->
        	<interceptor-ref name="defaultStack"/>
        	<param name="savePath">e:/Study/Struts</param>
        	<result name="success">/Success.jsp</result>
</action>

二、文件下载的时候

1、文件跳转(这里上传了文件的路径,名字,类型)

<s:iterator value="list" var="file">
		<a href="DownLoadAction?inputPath=${file.inputPath}&contentType=${file.contentType}&downFileName=${file.downFileName}">${file.downFileName}</a>
		<br>
</s:iterator>

2、后台处理(其中有很多转码问题)

private String inputPath;
	private String contentType;
	private String downFileName;
	public String getInputPath() {
		return inputPath;
	}
	public void setInputPath(String inputPath) throws UnsupportedEncodingException {
		this.inputPath = new String(inputPath.getBytes("iso-8859-1"),"utf-8");
		//this.inputPath = inputPath;
	}
	public String getContentType() {
		return contentType;
	}
	public void setContentType(String contentType) {
		this.contentType = contentType;
	}
	public String getDownFileName() throws UnsupportedEncodingException {
		// ת»»¸ñʽ
		downFileName = URLEncoder.encode(downFileName,"UTF-8");
		return downFileName;
	}
	public void setDownFileName(String downFileName) throws UnsupportedEncodingException {
		this.downFileName = new String(downFileName.getBytes("iso-8859-1"),"utf-8");
		//this.downFileName = downFileName;
	}
	public InputStream getTargetFile() throws FileNotFoundException
	{
		System.out.println(contentType+"###"+downFileName);
		System.out.println(inputPath+"ssssssssssssss");
		InputStream is = new FileInputStream(inputPath);   
		   
		return is;  
	}

3、后台Aciton书写

<result name="success" type="stream">
        		<param name="contentType">${contentType}</param>
        		<param name="inputName">targetFile</param>
        		<param name="contentDisposition">attachment;filename="${downFileName}"</param>
</result>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃货智

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值