// 這是我寫的一個(gè)方法,里面只需要傳兩個(gè)參數(shù)就OK了,在任何地方調(diào)用此方法都可以文件上傳
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了邗江免費(fèi)建站歡迎大家使用!
/**
* 上傳文件
* @param file待上傳的文件
* @param storePath待存儲(chǔ)的路徑(該路徑還包括文件名)
*/
public void uploadFormFile(FormFile file,String storePath)throws Exception{
// 開始上傳
InputStream is =null;
OutputStream os =null;
try {
is = file.getInputStream();
os = new FileOutputStream(storePath);
int bytes = 0;
byte[] buffer = new byte[8192];
while ((bytes = is.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytes);
}
os.close();
is.close();
} catch (Exception e) {
throw e;
}
finally{
if(os!=null){
try{
os.close();
os=null;
}catch(Exception e1){
;
}
}
if(is!=null){
try{
is.close();
is=null;
}catch(Exception e1){
;
}
}
}
}
在Java中可以用struts2實(shí)現(xiàn)多個(gè)文件同時(shí)上傳代碼,功能代碼如下:
1、upload.jsp頁面(選擇上傳文件)
form?action="upload.action"?name="uploadForm"?method="post"?enctype="multipart/form-data"
文件標(biāo)題:input?type="text"?name="title"/br/
選擇文件-:input?type="file"?name="upload"/br/
選擇文件二:input?type="file"?name="upload"/br/
選擇文件三:input?type="file"?name="upload"/br/
input?type="submit"?value="upload"/??
/form
2、action代碼如下:
//對應(yīng)的Action依次遍歷所有文件域,然后生成對應(yīng)的輸入文件流,輸出文件流在指定的服務(wù)器保存路徑中添加對應(yīng)的輸出文件流保存文件。同時(shí)動(dòng)態(tài)指定服務(wù)器上文件的保存路徑。
package?com.inspur.action;??
import?java.io.File;
import?java.io.FileInputStream;
import?java.io.FileOutputStream;?
import?org.apache.struts2.ServletActionContext;?
import?com.opensymphony.xwork2.ActionSupport;?
public?class?UploadAction?extends?ActionSupport?{
private?String?title;
private?File[]?upload;
private?String[]?uploadFileName;
private?String[]?uploadContentType;
private?String?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?getSavePath()?{
return?ServletActionContext.getRequest().getRealPath(savePath);
}
public?void?setSavePath(String?savePath)?{
this.savePath?=?savePath;
}
public?String?upload()throws?Exception{
File[]?files=this.getUpload();
for(int?i=0;ifiles.length;i++){
FileOutputStream?fos=new?FileOutputStream(this.getSavePath()+"\\"+this.getUploadFileName()[i]);
byte[]?buffer=new?byte[1024];
FileInputStream?fis=new?FileInputStream(files[i]);
int?len=0;
while((len=fis.read(buffer))0){
fos.write(buffer,0,len);
}
}?
return?SUCCESS;
}?
}
3、success.jsp頁面代碼如下(上傳成功界面顯示所有上傳的圖片)
文件標(biāo)題:s:property?value="title"/br/
第一個(gè)圖片:img?alt="first"?src="s:property?value="'upload/'+uploadFileName[0]"/"/br/
第二個(gè)圖片:img?alt="second"?src="s:property?value="'upload/'+uploadFileName[1]"/"/br/
public class FileUpLoad extends ActionSupport{
//"多文件上傳就用list就可以了private ListFile file;"
private File file;
//上傳文本的name
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
private String fileContentType;
//上傳的文件類型。
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
//獲取上傳文件的名稱
private String fileFileName;
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String upload() throws Exception
{
//獲取文件上傳路徑
String root=ServletActionContext.getRequest().getRealPath("/upload");
InputStream is=new FileInputStream(file);
String.substring(fileFileName.indexOf("."));//截取上傳文件的后綴。便于新定義名稱。.jpg
System.out.println(name);
File descFile=new File(root,新定義的文件名稱+fileFileName.indexOf("."));
OutputStream os=new FileOutputStream(descFile);
byte[] buffer=new byte[1024];
int length=0;
while(-1!=(length=(is.read(buffer))))
{
os.write(buffer, 0, length);
}
is.close();
os.close();
return SUCCESS;
}
}
網(wǎng)站標(biāo)題:java上傳代碼嗎,上傳文件Java
網(wǎng)頁鏈接:http://vcdvsql.cn/article40/heooeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、品牌網(wǎng)站制作、關(guān)鍵詞優(yōu)化、用戶體驗(yàn)、網(wǎng)站改版、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)