SmbFile(如何做共享⽂件上传功能)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.sources" value="message"></constant>
<package name="struts2" namespace="/pages/common" extends="json-default">
<action name="upfileAction" class="com.bq.web.action.UpfileAction">
<param name="fileSavePath">/upload</param>
<result type="json">
<param name="contentType">text/html</param>
</result>
<result name="fileList">/pages/common/commonUpfile.jsp</result>
<result name="fileListOEM">/pages/common/commonUpfileOEM.jsp</result>
<result name="error">/pages/error.jsp</result>
<result name="download" type="stream">/pages/common/commonUpfile.jsp</result>
<result name="success" type="stream">
<param name="contentType">application/x-msdownload;charset=ISO8859-1</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">
attachment;filename="${downloadChineseFileName}"
</param>
<param name="bufferSize">4096</param>
</result>
<result name="transfers" type="stream">
<param name="contentType">application/x-msdownload;charset=ISO8859-1</param>
<param name="inputName">transferInputStream</param>
<param name="contentDisposition">
attachment;filename="data.csv"
</param>
<param name="bufferSize">4096</param>
</result>
<result name="downloadvba" type="stream">
<param name="contentType">application/x-msdownload;charset=ISO8859-1</param>
<param name="inputName">outputStream</param>
<param name="contentDisposition">
attachment;filename="${filename}"
</param>
<param name="bufferSize">4096</param>
</result>
</action>
</package>
</struts>
public String getDownloadChineseFileName(){
HttpServletRequest request = Request();
HttpServletResponse response = Response();
String downloadChineseFileName = filename;
try {
OrderFileRecord r=new OrderFileRecord();
r.setOrderId(orderId);
r.setFileId(fileId);
List<OrderFileRecord> list=orderFileRecordService.queryOrderFileRecord(r);
if(list!=null&&list.size()>=0){
(0).getFileName();
}
//  response.setHeader("charset","ISO8859-1");
downloadChineseFileName= new Bytes("GB2312"), "ISO8859-1");
downloadChineseFileName=downloadChineseFileName;
//  if(downloadChineseFileName.length()>17){
//    filename=downloadChineseFileName.substring(downloadChineseFileName.lastIndexOf("."),downloadChineseFileName.length()); //    downloadChineseFileName=downloadChineseFileName.substring(0,13)+"..."+filename;
//  }
//downloadChineseFileName=new Bytes(), "UTF-8");
// downloadChineseFileName=de(downloadChineseFileName, "UTF-8");
/
/      response.setHeader( "Content-Disposition", "attachment;filename="  +  downloadChineseFileName);
}catch(Exception e){
e.printStackTrace();
}
return downloadChineseFileName;
}
public InputStream getInputStream() throws Exception{
File(getFileSavePath()+"/"+orderId+"/"+ fileId);
//  String filePath=getFileSavePath().replaceAll("\\\\", "/")+ "/" +orderId+"/"+ fileId;
//  FileInputStream fos = new FileInputStream(filePath);
//  return fos;
//  ServletContext().getResourceAsStream("/" + fileId);
}
public InputStream getTransferInputStream() throws Exception{
if(filename==null){
filename="";
}
placeAll("\"\r\n\"", "\"\r\n=\"");
InputStream  inputStream  =  new  Bytes());
//    new OutputStreamWriter(null,");
return inputStream;
}
getsavefilename
public InputStream getOutputStream() throws Exception{
InputStream  inputStream = null;
try{
inputStream = VBAFile( filename);
// File f = new File(SmbService.smbVBAFilePathConfig + filename);
// inputStream = new FileInputStream(f  )  ;
}catch(Exception e){
<(e.getMessage());
<(e.getStackTrace());
}
return inputStream;
}
public String downloadvba(){
// File f = new File(SmbService.smbVBAFilePathConfig + filename);
SmbFile f = null;
try {
f = new SmbFile(SmbService.smbVBAFilePathConfig+filename);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(f==null  ){
this.addActionError("File does not exit!");
return "error";
}
return "downloadvba";
}
public String transfers(){
HttpServletRequest request = Request ();
HttpServletResponse response= Response();
placeAll("\"\r\n\"", "\"\r\n=\"");
try
{
/
/如果無 out.clear(); 因重複會java.lang.IllegalStateException:
response.setContentType( "application/octet-stream");
response.setHeader("Content-disposition","attachment; filename=data.csv");
//response.setHeader will popup a screen ask where the new file save as, ⽤response.setHeader會出現popup問CSV會save去那  OutputStreamWriter ow = new OutputStream(),"GB2312");
//⽤outputStreamWriter set CSV TO UTF-16LE 這樣excel 開時不會有乱码問題
//  ow.write(0xFEFF);//⼀定要加
ow.write(filename);
ow.flush();
ow.close();
}catch(Exception e){
e.printStackTrace();
}
return null;
//  return "transfers";
}
public String execute() {
return "success";
}
public void setFileSavePath(String fileSavePath) {
this.fileSavePath = fileSavePath;
}
public void upload() throws Exception {
if (null == getOrderId() || "".equals(getOrderId())) {
return;
}
if (null == filename || "".equals(filename)) {
return;
}
if (null == xls) {
return;
}
OrderFileRecord r = new OrderFileRecord();
r.setFileName(filename);
r.setOrderId(orderId);
List l = orderFileRecordService.queryOrderFileRecord(r);
if (null != l && l.size() > 0) {
OrderFileRecord ofr = (OrderFileRecord) l.get(0);
FileId(), OrderId());
orderFileRecordService.deleteOrderFileRecord(ofr);
}
try {
uploadService.Xls(), orderId, getFileSavePath(),filename); //  SmbService.putFile(orderId+"/"+ fileId, Xls());
HttpServletResponse response = Response();
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
response.setHeader("Charset", "utf-8");
response.setHeader("Cache-Control", "no-cache");
} catch (Exception e) {
e.printStackTrace();
<(e);
return;
}
return;
}
String getJsonFileNameTableRow(){
if(getOrderId()==null||"".equals(getOrderId())){
return "";
}
OrderFileRecord r=new OrderFileRecord();
r.setOrderId(getOrderId());
List l=orderFileRecordList=orderFileRecordService.queryOrderFileRecord(r);
if(null==l||l.size()>0){
OrderFileRecord vd=((0);
}
return "";
}
public String deleteRecord(String fileId,String orderId){
if(fileId==null||"".equals(fileId)){
return queryOrderFile();
}else if(getOrderId()==null||"".equals(getOrderId())){
}
im();
im();
String filePath=getFileSavePath()+"/"+orderId+"/"+ fileId;
/
/  String filePath=getFileSavePath().replaceAll("\\\\", "/")+ "/" +orderId+"/"+ fileId; //  deleteFile(filePath);
SmbService.deleteFile(filePath);
OrderFileRecord r=new OrderFileRecord();
r.setOrderId(getOrderId());
r.setFileId(fileId);
orderFileRecordService.deleteOrderFileRecord(r);
return queryOrderFile();
}
public String deleteRecordOEM(String fileId,String orderId){
if(fileId==null||"".equals(fileId)){
return queryOrderFile();
}else if(getOrderId()==null||"".equals(getOrderId())){
return queryOrderFile();
}
im();
im();
String filePath=getFileSavePath()+"/"+orderId+"/"+ fileId;
//  String filePath=getFileSavePath().replaceAll("\\\\", "/")+ "/" +orderId+"/"+ fileId; //  deleteFile(filePath);
SmbService.deleteFile(filePath);
OrderFileRecord r=new OrderFileRecord();
r.setOrderId(getOrderId());
r.setFileId(fileId);
orderFileRecordService.deleteOrderFileRecord(r);
return queryOrderFileOEM();
}
public String deleteRecord(){
if (null != orderId) {
im();
}
if (null == fileId) {
im();
}
return deleteRecord(fileId,orderId);
}
public String deleteRecordOEM(){
if (null != orderId) {
im();
}
if (null == fileId) {
im();
}
return deleteRecordOEM(fileId,orderId);
}
@Deprecated

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。