一、文件上傳
在進行文件上傳的時候我們需要注意這麼幾個點
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>