struts2 解决上传问题

记录下最近接触的用struct上传文件的方法。

 

  • html
符号表:<input type="file" id="fhb" name="binfile" style="cursor:pointer;" οnchange="fileChange(this,'fhb');"/>
bin文件:<input type="file" id="bin" name="binfile" style="cursor:pointer;" οnchange="fileChange(this,'bin');"/>

补丁文件:<input type="file" id="bdwj1" name="upload" style="cursor:pointer;" οnchange="fileChange(this,'bdwj1');"/>

 

  •   action
public class UploadMoreAction extends ActionSupport {
	private static final int NUM = 8192;

	// 补丁文件
	private List<String> uploads;
	private List<String> fileNames;
	private List<String> uploadContentTypes;

	//bin和符号表
	private List<String> binfiles;
	private List<String> binfileFileNames;
	private List<String> binfileContentTypes;

	public void uploadFile() {
		if (this.getUpload() != null) {
			int i = 0;
			for (; i < this.getUpload().size(); i++) {
				InputStream is = new FileInputStream((String) this
						.getUpload().get(i));
				OutputStream os = new FileOutputStream("D:\\test"
						+ this.getFileNames().get(i));
				byte buffer[] = new byte[NUM];
				int count = 0;
				while ((count = is.read(buffer)) > 0) {
					os.write(buffer, 0, count);
				}
				os.close();
				is.close();
			}
		}
	}
	
	public void uploadBINSTFile() {
		if (this.getBinfile() != null) {
			int i = 0;
			for (; i < this.getBinfile().size(); i++) {
				InputStream is = new FileInputStream((String) this
						.getBinfile().get(i));
				OutputStream os = new FileOutputStream("D:\\test"
						+ this.getBinfileFileName().get(i));
				byte buffer[] = new byte[NUM];
				int count = 0;
				while ((count = is.read(buffer)) > 0) {
					os.write(buffer, 0, count);
				}
				os.close();
				is.close();
			}
		}
	}
	public List<String> getUploadFileName() {
		return fileNames;
	}
	public void setUploadFileName(List<String> fileNames) {
		this.fileNames = fileNames;
	}
	public List<String> getUpload() {
		return uploads;
	}
	public void setUpload(List<String> uploads) {
		this.uploads = uploads;
	}
	public void setUploadContentType(List<String> contentTypes) {
		this.uploadContentTypes = contentTypes;
	}
	public List<String> getUploadContentType() {
		return this.uploadContentTypes;
	}
	public List<String> getFileNames() {
		return fileNames;
	}
	public void setFileNames(List<String> fileNames) {
		this.fileNames = fileNames;
	}
	public List<String> getBinfile() {
		return binfiles;
	}
	public void setBinfile(List<String> binfiles) {
		this.binfiles = binfiles;
	}
	public List<String> getBinfileFileName() {
		return binfileFileNames;
	}
	public void setBinfileFileName(List<String> binfileFileNames) {
		this.binfileFileNames = binfileFileNames;
	}
	public List<String> getBinfileContentType() {
		return binfileContentTypes;
	}
	public void setBinfileContentType(List<String> binfileContentTypes) {
		this.binfileContentTypes = binfileContentTypes;
	}
}
  • struts.xml
<action name="upload_*" class="com.UploadMoreAction" method="{1}">
		<interceptor-ref name="fileUpload">
		
			<!-- 配置允许上传的文件类型,多个用","分隔 -->
			<param name="allowedTypes">
				<!--image/bmp,image/png,image/gif,image/jpeg,image/jpg-->
			</param>
			
			<!-- 配置允许上传的文件大小,单位字节-->
			<param name="maximumSize">5000000000000000</param>  
		</interceptor-ref>
		
		<interceptor-ref name="defaultStack" />
		<result name="success">success.jsp</result>
</action>

 

  • 为了明了其中蹊跷,debug跟了下其中的值
  • 补丁文件上传了名为test.txt, test2.txt的文本文件
  • 符号表和bin文件上传了名为st.txt, bin.txt的文本文件
字段
uploads[\tmp\upload__1fea43f6_133af286d4d__8000_00000012.tmp, \tmp\upload__1fea43f6_133af286d4d__8000_00000013.tmp]
fileNames[test.txt, test2.txt]
uploadContentTypes [text/plain, text/plain]
binfiles[\tmp\upload__1fea43f6_133af286d4d__8000_00000010.tmp, \tmp\upload__1fea43f6_133af286d4d__8000_00000011.tmp] 
binfileFileNames[st.txt, bin.txt]
binfileContentTypes[text/plain, text/plain]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值