📄 fileuploadbean.java
字号:
package com.windy.fileupload;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.List;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.component.html.HtmlInputHidden;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.myfaces.custom.fileupload.UploadedFile;
public class FileUploadBean {
private UploadedFile myFile;
DBConnection conn=new DBConnection();
public UploadedFile getMyFile() {
return myFile;
}
public void setMyFile(UploadedFile myFile) {
this.myFile = myFile;
}
public void action(String filename,String path){
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs","sa","sa");
PreparedStatement stm=conn.prepareStatement("insert into filelist(filename,filepath)values(?,?)");
stm.setString(1,filename);
stm.setString(2,path);
stm.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
public String processFile(){
try {
long time=System.currentTimeMillis();
String fileName=new File(myFile.getName()).getName();
System.out.println(fileName);
InputStream in=myFile.getInputStream();
FacesContext ctx=FacesContext.getCurrentInstance();
HttpSession hs=(HttpSession)ctx.getExternalContext().getSession(true);
String directory=hs.getServletContext().getRealPath("\\")+"upload";
File fileDir=new File(directory);
if(!fileDir.exists()){
if(!fileDir.mkdir()){
System.out.println("文件夹创建失败");
}
}
String file=hs.getServletContext().getRealPath("\\")+"upload\\"+fileName;
this.action(fileName,file);
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(file));
byte[] buffer=new byte[1024*2];
int length;
while((length=in.read(buffer,0,buffer.length))>0){
out.write(buffer,0,length);
}
out.flush();
out.close();
long t=System.currentTimeMillis()-time;
return "success";
} catch (Exception e) {
e.printStackTrace();
return "error";
}
}
public List getFileList(){
return conn.getFile();
}
public String getpath(){
FacesContext ctx = FacesContext.getCurrentInstance();
HtmlInputHidden inputtextValue = (HtmlInputHidden)ctx.getViewRoot().findComponent("frm:filepath");
return (String) (inputtextValue.getValue());
}
public String download() {
FacesContext ctx = FacesContext.getCurrentInstance();
HttpServletRequest request=(HttpServletRequest)ctx.getExternalContext().getRequest();
String filepath=conn.getfilepath(Integer.parseInt(request.getParameter("fileid")));
if(!new File(filepath).exists()){
System.out.println("文件不存在...........");
return "";
}
try {
String contentType = "application/octet-stream;charset=iso8859-1";
HttpServletResponse response = (HttpServletResponse) ctx
.getExternalContext().getResponse();
response.setContentType(contentType);
StringBuffer contentDisposition = new StringBuffer();
contentDisposition.append("attachment;");
contentDisposition.append("filename=\"");
contentDisposition.append(new File(filepath).getName());
contentDisposition.append("\"");
//logger.debug(System.getProperty("file.encoding"));
response.setHeader("Content-Disposition", new String(
contentDisposition.toString().getBytes(
System.getProperty("file.encoding")), "iso8859-1"));
//logger.debug(contentDisposition.toString());
OutputStream out = response.getOutputStream();
byte[] bytes = new byte[1024];
InputStream is = new FileInputStream(filepath);
int b = 0;
while ((b = is.read(bytes, 0, 1024)) > 0) {
out.write(bytes, 0, b);
}
is.close();
out.flush();
ctx.responseComplete();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public void execute(ActionEvent event){
HtmlDataTable datatable=(HtmlDataTable)event.getComponent();
System.out.println(datatable.getRowData());
}
public void printDirectory(){
FacesContext ctx=FacesContext.getCurrentInstance();
HttpServletRequest req=(HttpServletRequest)ctx.getExternalContext().getRequest();
String directory=req.getRealPath("/")+"upload";
File file=new File(directory);
String[] files=file.list();
System.out.println(">>>>>>>>>>>>>>>>>>>>>");
for (int i = 0; i < files.length; i++) {
System.out.println(files[i]);
}
System.out.println(">>>>>>>>>>>>>>>>>>>>>");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -