private ProgressBar mPgBar; class UploadTask extends AsyncTask<Object,Integer,Void>{ private DataOutputStream outputStream = null; private String fileName; private String uri; private String mLineEnd = " "; private String mTwoHyphens = "--"; private String boundary = "*****"; File uploadFile ; long mTtotalSize ; // Get size of file, bytes public UploadTask(String fileName,String uri){this.fileName = fileName;this.uri = uri;uploadFile= new File(fileName); mTtotalSize = uploadFile.length(); }/*** 开始上传文件* @param objects* @return*/ @Override protected Void doInBackground(Object... objects) {long length = 0;int mBytesRead, mbytesAvailable, mBufferSize;byte[] buffer;int maxBufferSize = 256 * 1024;// 256KBtry{FileInputStream fileInputStream = new FileInputStream(new File(fileName));URL url = new URL(uri);HttpURLConnection con = (HttpURLConnection) url.openConnection();//如果有必要则可以设置Cookie //conn.setRequestProperty("Cookie","JSESSIONID="+cookie);// Set size of every block for postcon.setChunkedStreamingMode(256 * 1024);// 256KB// Allow Inputs & Outputs con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false);// Enable POST method con.setRequestMethod("POST"); con.setRequestProperty("Connection", "Keep-Alive"); con.setRequestProperty("Charset", "UTF-8"); con.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);outputStream = new DataOutputStream( con.getOutputStream()); outputStream.writeBytes(mTwoHyphens + boundary + mLineEnd); outputStream.writeBytes("Content-Disposition: form-data; name="file"; filename="" + fileName + """ + mLineEnd); outputStream.writeBytes("Content-Type:application/octet-stream "); outputStream.writeBytes(mLineEnd);mbytesAvailable = fileInputStream.available(); mBufferSize = Math.min(mbytesAvailable, maxBufferSize); buffer = new byte[mBufferSize];// Read file mBytesRead = fileInputStream.read(buffer, 0, mBufferSize);while (mBytesRead > 0) {outputStream.write(buffer, 0, mBufferSize);length += mBufferSize; publishProgress((int) ((length * 100) / mTtotalSize)); mbytesAvailable = fileInputStream.available(); mBufferSize = Math.min(mbytesAvailable, maxBufferSize); mBytesRead = fileInputStream.read(buffer, 0, mBufferSize); } outputStream.writeBytes(mLineEnd); outputStream.writeBytes(mTwoHyphens + boundary + mTwoHyphens + mLineEnd); publishProgress(100);// Responses from the server (code and message) int serverResponseCode = con.getResponseCode(); String serverResponseMessage = con.getResponseMessage(); fileInputStream.close(); outputStream.flush(); outputStream.close(); } catch (Exception ex) { ex.printStackTrace(); Log.v(TAG,"uploadError");}return null; }@Override protected void onProgressUpdate(Integer... progress) {mPgBar.setProgress(progress[0]); }}主要流程为继承AsyncTask,然后使用HttpURLConnection 去上传文件。代码比较简单,就不一一讲解了。
//上传文件集合private List<File> file;//上传文件名集合private List<String> fileFileName;//上传文件内容类型集合private List<String> fileContentType; public List<File> getFile() { return file;} public void setFile(List<File> file) { this.file = file;} public List<String> getFileFileName() { return fileFileName;} public void setFileFileName(List<String> fileFileName) { this.fileFileName = fileFileName;} public List<String> getFileContentType() { return fileContentType;} public void setFileContentType(List<String> fileContentType) { this.fileContentType = fileContentType;}采用了多文件上传的方法,定义了List 集合。
public String testUpload()throws Exception{ System.out.println("success"); uploadFile(0); return SUCCESS;}到这里就已经才不多完成动作了,现在需要开始写上传的方法 uploadFile(int index),由于定义file 为多文件上传,而我们上传只上传了一个文件,所以这里参数为0
/** * 上传功能 * @param i * @return * @throws FileNotFoundException * @throws IOException */private String uploadFile(int i) throws FileNotFoundException, IOException {try {InputStream in = new FileInputStream(file.get(i)); //String dir = ServletActionContext.getRequest().getRealPath(UPLOADDIR);String dir = "D://UploadData/"; File uploadFile = new File(dir,StringUtils.getUUID()+getFile( this.getFileFileName().get(i))); OutputStream out = new FileOutputStream(uploadFile); byte[] buffer = new byte[1024 * 1024]; int length;while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length);} in.close();out.close();//然后进行计算return uploadFile.getAbsolutePath(); } catch (FileNotFoundException ex) {ex.printStackTrace(); } catch (IOException ex) {ex.printStackTrace(); } return null;}上面方法为将缓存区域的文件 然后搞到了D://UploadData/ 文件中,然后以自己的格式进行命名,这里我使用了电脑的UUID和文件名进行组合,确保我复制过来的文件不重复。
<!-- 系统常量定义,定义上传文件字符集编码 --><constant name="struts.i18n.encoding" value="utf-8"></constant><!-- 系统常量定义,定义上传文件零时存放路径 --><constant name="struts.multipart.saveDir" value="c: mp"></constant><constant name="struts.multipart.maxSize" value="10000000" />这里主要定义上传文件的临时存放位置,然后大小限制。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。