记录下最近接触的用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] |